Read Also: Difference between float and double datatypes in Java
Java long vs double
1. Definition
According to Oracle docs, the long data type is a 64-bit two's complement integer whereas the double data type is a double-precision 64-bit IEEE 754 floating point.
2. Default value
Another difference between long and double in Java is that the default value of long is 0L whereas the default value of double is 0.0d.
3. Symbol
The value of long ends with "L" whereas the value of double ends with "d". For example,
long num = 1234567L;
double num2 = 12345.67d;
4. Range of values
Moreover, one other difference between long and double in Java is that long stores whole numbers from -9223372036854775808 to 9223372036854775807 whereas double stores values from 1.7e-308 to 1.7e+038.
5. Type
Furthermore, the data type long is an integral type whereas the data type double is a floating point type.
Example of long and double datatypes in Java
public class LongDoubleExample {
public static void main(String args[]) {
long num = 13022013L;
System.out.println(num);
double num2 = 23042.013d;
System.out.println(num2);
}
}
Output:
13022013
23042.013
Recap: Difference between long and double in Java
long | double | |
---|---|---|
Definition | 64-bit two's complement integer | double-precision 64-bit IEEE 754 floating point |
Default value | 0L | 0.0d |
Symbol | value ends with "L" | value of double ends with "d" |
Range | -9223372036854775808 to 9223372036854775807 | 1.7e-308 to 1.7e+038 |
Type | integral type | floating point type |
That's all for today. Please mention in the comments if you have any questions related to the difference between long and double datatypes in Java with examples.