Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double vs doubleValue in Java

Tags:

java

if a method returns a Double, why would we call the method "doubleValue" on it? it already returns a double and in calculations, appears to evaluate correctly.

like image 270
MedicineMan Avatar asked Jan 03 '12 03:01

MedicineMan


People also ask

What does double doubleValue do in Java?

The java. lang. Double. doubleValue() method returns the double value of this Double object.

What is doubleValue ()?

doubleValue() is an inbuilt method in java that returns the value of the specified number casted as a double data type. This may involve rounding or truncation. Syntax: public abstract double doubleValue()

What does double parseDouble mean in Java?

Double parseDouble() method in Java with examples The parseDouble() method of Java Double class is a built in method in Java that returns a new double initialized to the value represented by the specified String, as done by the valueOf method of class Double.


2 Answers

Prior to Java 1.5 auto-boxing (and unboxing) didn't exist in Java. So, you would need this to extract the underlying primitive from a Double.

If you are unfamiliar with auto-boxing, you can read more here. http://docs.oracle.com/javase/1.5.0/docs/guide/language/autoboxing.html

like image 160
rfeak Avatar answered Sep 19 '22 19:09

rfeak


You call 'doubleValue' on a Double object to convert from the boxed Object to the primitive data type. Since most of the time the Double is auto-unboxed to a double, you usually don't need to do this, but if you want to be explicit in your conversion, you can call this method.

like image 42
I82Much Avatar answered Sep 23 '22 19:09

I82Much