看起来 double 类型变量没有方法。 Java 或 NetBeans 有问题吗?
根据 Oracle 的说法,我应该能够将 .intValue()
和 .compareTo()
等方法应用于双精度,但是当我编写 dbl.toString()(Integer) dbl
形式的整数!
我有 JDK 1.6 和 NetBeans 6.9.1。这里有什么问题?
According to Oracle I should be able to apply methods like .intValue()
and .compareTo()
to doubles but when I write dbl.toString()
in NetBeans, for example, the IDE tells me that doubles can't be dereferenced. I can't even cast them to Integers in the form (Integer) dbl
!
I have JDK 1.6 and NetBeans 6.9.1. What's the problem here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您提到的方法可以在 Double 类中找到(而不是在 double 基元类型中找到)。使用基本类型总是更有效,但如果您绝对需要这些方法,请创建一个新的 Double 对象,如下所示:
The methods you're mentioning are found on the Double class (and not in the double primitive type). It's always more efficient to use primitive types, but if you absolutely need those methods, create a new Double object like this:
问题在于您对对象与基元的理解。
最重要的是,您只需要认识到大写名称是像基元一样的对象类,只有当您需要将数据从基元发送到仅接受对象的方法时才需要它们。您的转换失败,因为您试图将一个基元(double)转换为一个对象(Integer)而不是另一个基元(int)。
以下是使用基元与对象的一些示例:
Double 类有一个静态方法 toString():
其他方法可以直接使用运算符而不是调用方法。
The problem is your understanding of objects vs. primitives.
More than anything else, you just need to recognize that the capitalized names are object classes that act like primitives, which are really only necessary when you need to send data from primitives into a method that only accepts objects. Your cast failed because you were trying to cast a primitive (double) to an object (Integer) instead of another primitive (int).
Here are some examples of working with primitives vs objects:
The Double class has a static method toString():
Other methods can use operators directly rather than calling a method.
double 是原语而不是对象。因此它没有方法。一般来说,您想要做的是使用类似的语言。
也许如果您说出您想要实现的目标,我们可以向您指出可以实现该目标的 Java 代码。
double is a primitive not an Object. As such it has no methods. Generally what you want to do is use the language like.
Perhaps if you say what you are trying to achieve we can point you to the Java code which will do that.
每个对象都有一个 toString 方法,因此可能您的 JDK 在 NetBeans 中配置不正确。
Every object has a toString method, so maybe your JDK is not configured properly in NetBeans.
您想要
(java.lang.)Double
而不是原始的double
You want
(java.lang.)Double
not the primitivedouble
本机类型(
int
、float
、double
等)没有方法。Native types (
int
,float
,double
, etc) do not have methods.