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?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
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.
You want
(java.lang.)Double
not the primitivedouble
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.
Every object has a toString method, so maybe your JDK is not configured properly in NetBeans.
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:
Native types (
int
,float
,double
, etc) do not have methods.