看起来 double 类型变量没有方法。 Java 或 NetBeans 有问题吗?

发布于 2024-10-09 12:12:03 字数 244 浏览 8 评论 0原文

根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

∞梦里开花 2024-10-16 12:12:03

您提到的方法可以在 Double 类中找到(而不是在 double 基元类型中找到)。使用基本类型总是更有效,但如果您绝对需要这些方法,请创建一个新的 Double 对象,如下所示:

double d = 10.0;
Double myDouble = new Double(d);

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 d = 10.0;
Double myDouble = new Double(d);
在梵高的星空下 2024-10-16 12:12:03

问题在于您对对象与基元的理解。

最重要的是,您只需要认识到大写名称是像基元一样的对象类,只有当您需要将数据从基元发送到仅接受对象的方法时才需要它们。您的转换失败,因为您试图将一个基元(double)转换为一个对象(Integer)而不是另一个基元(int)。

以下是使用基元与对象的一些示例:

Double 类有一个静态方法 toString():

double d = 10.0;
// wrong
System.out.println(d.toString());
// instead do this
System.out.println(Double.toString(d));

其他方法可以直接使用运算符而不是调用方法。

double a = 10.0, b = 5.0;

// wrong
if( a.compareTo(b) > 0 ) { /* ... */ }
// instead you can simply do this:
if( a >= b) { /* ... */ }

int a = 0;
double b = 10.0;

// wrong
a = b.intValue();
// perform the cast directly.
a = (int)b;

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():

double d = 10.0;
// wrong
System.out.println(d.toString());
// instead do this
System.out.println(Double.toString(d));

Other methods can use operators directly rather than calling a method.

double a = 10.0, b = 5.0;

// wrong
if( a.compareTo(b) > 0 ) { /* ... */ }
// instead you can simply do this:
if( a >= b) { /* ... */ }

int a = 0;
double b = 10.0;

// wrong
a = b.intValue();
// perform the cast directly.
a = (int)b;
城歌 2024-10-16 12:12:03

double 是原语而不是对象。因此它没有方法。一般来说,您想要做的是使用类似的语言。

double d = 1.1;
System.out.println("d= "+d); // calls Double.toString(d) for you.

int i = (int) d; // more efficient than new Double(d).intValue();

if (d >= 1.0) // does a compare.

也许如果您说出您想要实现的目标,我们可以向您指出可以实现该目标的 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.

double d = 1.1;
System.out.println("d= "+d); // calls Double.toString(d) for you.

int i = (int) d; // more efficient than new Double(d).intValue();

if (d >= 1.0) // does a compare.

Perhaps if you say what you are trying to achieve we can point you to the Java code which will do that.

小镇女孩 2024-10-16 12:12:03

每个对象都有一个 toString 方法,因此可能您的 JDK 在 NetBeans 中配置不正确。

Every object has a toString method, so maybe your JDK is not configured properly in NetBeans.

森林迷了鹿 2024-10-16 12:12:03

您想要 (java.lang.)Double 而不是原始的 double

You want (java.lang.)Double not the primitive double

风铃鹿 2024-10-16 12:12:03

本机类型(intfloatdouble 等)没有方法。

Native types (int, float, double, etc) do not have methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文