In Java, double and Double are two different things.
double is a primitive data type, while Double is a class.
A primitive data type is a basic data type that is built into the language. A class is a user-defined data type that can be created by the programmer.
A double variable stores a floating-point number. A Double object stores a reference to a floating-point number.
Here is an example:
Java
double d = 1.23;
Double obj = new Double(1.23);
In this example, d is a double variable that stores the floating-point number 1.23.
obj is a Double object that stores a reference to the same floating-point number.
The main difference between double and Double is that
double is a value type, while Double is a reference type. This means that
double variables are stored directly on the stack, while Double objects 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 double when you need to store a floating-point number and you do not need the flexibility of a reference type. You should use
Double when you need to store a floating-point number and you need the flexibility of a reference type.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In 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.