Java long vs double

In this post, I will be sharing what is the difference between long and double in Java. Both are primitive data types. Let's dive deep into the topic:

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


longdouble
Definition64-bit two's complement integerdouble-precision 64-bit IEEE 754 floating point
Default value0L0.0d
Symbolvalue ends with "L"value of double ends with "d"
Range-9223372036854775808 to 92233720368547758071.7e-308 to 1.7e+038
Typeintegral typefloating 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.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry