A double is a Java data type used to store decimal (floating-point) numbers.
double count = 20.1;
double temperature = -9.5;
Memory size and range
A double uses 64 bits (8 bytes) of memory. It can store very small and very large numbers:
- Smallest positive value:
4.9E-324 - Largest value:
1.7976931348623157E308
E means “× 10 to the power of”. Example: 1.5E2 = 150
double vs float
Java has two decimal number types:
float(32 bits)double(64 bits)
double is more precise and is the default choice when working with decimal numbers.
double price = 19.99; // preferred
float tax = 0.2f; // less precise
Primitive Type
A double is a primitive data type which means:
- It stores the actual value directly in memory
- It is not an object
- It has no methods of its own
Primitive types do not change internally. When you assign a new value, the old value is replaced.
double a = 15.2;
a = 20.7; // 15.2 is replaced with 20.7
There is no memory reference to update — just a new value stored.
Double Wrapper Class
Although double has no methods, Java provides a wrapper class called Double. This allows you to:
- Convert values
- Compare values
- Access useful constants
double x = 3.14;
String text = Double.toString(x); // Value of text is "3.14"
Note: These methods must be applied to the Double wrapper class. Applying them to a double primitive will generate an error
Double Constants
| Constant | Example | Result |
| MAX_VALUE | Double.MAX_VALUE | 1.7976931348623157E308 |
| MIN_VALUE | Double.MIN_VALUE | 4.9E-324 |
| SIZE | Double.SIZE | Size in bits (64) |
| BYTES | Double.BYTES | Size in bytes (8) |
Converting values
Converting a numeric String to a Double. A non-numeric String generates an error.
double num = Double.parseDouble("123.5"); // Returns double 123.5
Double.parseDouble("hello"); // Returns NumberFormatException
Converting a Double to a String
String text = Double.toString(42.0); // Returns "42.0"
Comparing double values
compare
Double.compare(1.5, 2.5);
compareTo
Double x = 10.0;
Double y = 12.2;
x.compareTo(y);
In both cases the return values are:
1→ if first value is greater0→ if both values are equal-1→ if first value is smaller
equals
x.equals(y); // Returns true or false
Math helper methods
Double.max(1.5, 2.5); // Returns greater figure - 2.5
Double.min(1.5, 2.5); // Returns lesser figure - 1.5
Double.sum(1.5, 2.5); // Sums the two doubles - 4.0
Summary
doublestores decimal numbers- double uses 64 bits / 8 bytes
- double is more precise than float
- Is a primitive type
- It uses the Double wrapper class to access methods
Leave a Reply