看起来像double类型的变量没有方法。 Java或NetBeans有问题吗? [英] Looks like double type variables have no methods. Something wrong with Java or NetBeans?

查看:196
本文介绍了看起来像double类型的变量没有方法。 Java或NetBeans有问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据Oracle,我应该可以应用诸如 .intValue() .compareTo()之类的方法双打,但是当我在NetBeans中写 dbl.toString()时,IDE会告诉我,双精度不能被取消引用。我甚至不能以Integer的形式将它们转换成Integers,它们的格式为$ {code}(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?

推荐答案

问题是您对对象与原语的理解。

The problem is your understanding of objects vs. primitives.

除了别的以外,你只需要认识到大写的名字就是像原始图像那样的对象类,这些只是在需要将数据从原语发送到方法只接受对象。你的演员失败了,因为你试图将一个原始(双重)转换成一个对象(整数),而不是另一个原语(int)。

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:

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

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;

这篇关于看起来像double类型的变量没有方法。 Java或NetBeans有问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆