What is the difference between "Double" and "double" in Java?
What is the difference between "Double" and "double" in Java?
I completed my post-graduation in 2013 in the engineering field. Engineering is the application of science and math to solve problems. Engineers figure out how things work and find practical uses for scientific discoveries. Scientists and inventors often get the credit for innovations that advance the human condition, but it is engineers who are instrumental in making those innovations available to the world. I love pet animals such as dogs, cats, etc.
Aryan Kumar
19-Aug-2023In Java,
doubleandDoubleare two different things.doubleis a primitive data type, whileDoubleis a class.doublevariable stores a floating-point number. ADoubleobject stores a reference to a floating-point number.Here is an example:
Java
In this example,
dis adoublevariable that stores the floating-point number 1.23.objis aDoubleobject that stores a reference to the same floating-point number.The main difference between
doubleandDoubleis thatdoubleis a value type, whileDoubleis a reference type. This means thatdoublevariables are stored directly on the stack, whileDoubleobjects are stored on the heap.Reference types are more complex than value types and require more memory. However, they also offer more flexibility, as they can be used to store null values and they can be used in generics.
In general, you should use
doublewhen you need to store a floating-point number and you do not need the flexibility of a reference type. You should useDoublewhen you need to store a floating-point number and you need the flexibility of a reference type.