Double vs doubleValue in Java

if the method returns Double, why will we call it "doubleValue"? it already returns double and in calculations, it seems, correctly evaluates.

+6
source share
4 answers

Prior to Java 1.5, automatic boxing (and unboxing) did not exist in Java. So, you will need this to extract the basic primitive from Double.

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

+9
source

You call 'doubleValue' on a Double Object to convert from a placed object to a primitive data type. Since most of the time Double is automatically decompressed into double, you usually do not need to do this, but if you want to be explicit in your conversion, you can call this method.

+1
source

You would call doubleValue if you need to get double from double . The former is a primitive type, and the latter is a wrapper object type that basically encapsulates a primitive value inside an immutable object. If possible (and I say this without knowing exactly the purpose of your code), use double for all your calculations; use only double if you need to save values ​​in Collection or Map ; that, since object types consume more memory and can be casted.

It's easy to accept double with double (the same applies to other primitive types versus wrapper types), since the compiler will automatically convert from one to another as necessary in a process known as automatic boxing (and automatic unpacking), so a little take care to use the correct type.

+1
source

doubleValue returns a primitive double from a double object. Depending on the scenario, you would like to get double from Double.

+1
source

Source: https://habr.com/ru/post/904954/


All Articles