double is made of 1 bit sign 52 bit mantissa and 11 bit exponent and long is made of 1 bit sign and 63 bit number. So double can have larger values, and they both take 64 bits in your memory. So double has the precision of at most 16 digits and long of at most 19 digits. (Also, a double can have a value of Infinity) If you're looking for an object that can store very large numbers, use BigInteger or BigDecimal, their value is only limited by how much memory your computer has and they can do precise math. Answer from Chemical-Asparagus58 on reddit.com
Top answer
1 of 3
26

If you are storing integers, use Long. Your statement that "Advantage of Using Double is that it gives a more wider range for storing Whole Numbers" is incorrect. Both are 64 bits long, but double has to use some bits for the exponent, leaving fewer bits to represent the magnitude. You can store larger numbers in a double but you will lose precision.

In other words, for numbers larger than some upper bound you can no longer store adjacent "whole numbers"... given an integer value above this threshold, the "next" possible double will be more than 1 greater than the previous number.

For example

public class Test1  
{

    public static void main(String[] args) throws Exception 
    {
        long   long1 = Long.MAX_VALUE - 100L;
        double dbl1  = long1;
        long   long2 = long1+1;
        double dbl2  = dbl1+1;
        double dbl3  = dbl2+Math.ulp(dbl2);

        System.out.printf("%d %d\n%f %f %f", long1, long2, dbl1, dbl2, dbl3);
    }

}

This outputs:

9223372036854775707 9223372036854775708
9223372036854776000.000000 9223372036854776000.000000 9223372036854778000.000000

Note that

  1. The double representation of Long.MAX_VALUE-100 does NOT equal the original value
  2. Adding 1 to the double representation of Long.MAX_VALUE-100 has no effect
  3. At this magnitude, the difference between one double and the next possible double value is 2000.

Another way of saying this is that long has just under 19 digits precision, while double has only 16 digits precision. Double can store numbers larger than 16 digits, but at the cost of truncation/rounding in the low-order digits.

If you need more than 19 digits precision you must resort to BigInteger, with the expected decrease in performance.

2 of 3
3

This looks like the wrong battle:

From the Java Tutorial

The long data type is a 64-bit signed two's complement integer. It has a minimum value of -9,223,372,036,854,775,808 and a maximum value of 9,223,372,036,854,775,807 (inclusive).

That's pretty close to 19 significant digits

From Wikipedia

This gives from 15 - 17 significant decimal digits precision.

So, despite its apparent "superiority" Double will serve you worse than Long. And I'm merely guessing here, but intuitively I'd say serialization/deserialization of floating point types are costlier operations than the same operations on integral data types, but even if there are differences they will be quite small on modern systems.

So, when working with integers, stick to Long.

🌐
Coderanch
coderanch.com › t › 328469 › java › comparision-double-long
comparision between double and long (Java in General forum at Coderanch)
Rizwan SCJA, SCJP, SCWCD, SCBCD, SCDJWS. ... If you remove string and give numbers as double instead of string it says equal(compare method returns 0) BigDecimal bd1 = new BigDecimal(1234567891.00000001); BigDecimal bd2 = new BigDecimal(1234567891); bd1.compareTo(bd2) returns 0.
🌐
Pediaa
pediaa.com › home › technology › it › programming › what is the difference between long and double in java
What is the Difference Between long and double in Java - Pediaa.Com
May 30, 2019 - Moreover, one other difference ... point type. Additionally, long var1 = 1000000L is an example for long data type while double var2 = 10.3 or double var2=10.3d is an example for double data type....
🌐
RETIT
retit.de › performance-in-detail-long-vs-double-in-java-2
Performance in Detail: Long vs. Double in Java – RETIT
April 23, 2018 - A few weeks ago, we stumbled upon an interesting problem during a code refactoring. After changing the data type of an attribute from List<Long> to List<Double>, we noticed some severe performance issues. Our application took minutes to process data it previously handled in seconds.
🌐
Blogger
javahungry.blogspot.com › 2022 › 11 › long-vs-double.html
Java long vs double | Java Hungry
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. The value of long ends with "L" whereas the value of double ends with "d". For example,
Top answer
1 of 4
7

The absolute quantity of information that you can store in 64 bit is of course the same.

What changes is the meaning you assign to the bits.

In an integer or long variable, the codification used is the same you use for decimal numbers in your normal life, with the exception of the fact that number two complement is used, but this doesn't change that much, since it's only a trick to gain an additional number (while storing just one zero instead that a positive and a negative).

In a float or double variable, bits are split in two kinds: the mantissa and the exponent. This means that every double number is shaped like XXXXYYYYY where it's numerical value is something like XXXX*2^YYYY. Basically you decide to encode them in a different way, what you obtain is that you have the same amount of values but they are distribuited in a different way over the whole set of real numbers.

The fact that the largest/smallest value of a floating number is larger/smaller of the largest/smalles value of a integer number doesn't imply anything on the amount of data effectively stored.

2 of 4
2

A double can store a larger number by having larger intervals between the numbers it can store, essentially. Not every integer in the range of a double is representable by that double.

More specifically, a double has one bit (S) to store sign, 11 bits to store an exponent E, and 52 bits of precision, in what is called the mantissa (M).

For most numbers (There are some special cases), a double stores the number (-1)^S * (1 + (M * 2^{-52})) * 2^{E - 1023}, and as such, when E is large, changing M by one will make a much larger change in the size of the resulting number than one. These large gaps are what give doubles a larger range than longs.

🌐
Quora
quora.com › What-is-the-difference-between-an-int-a-long-a-double-and-a-decimal-in-Java
What is the difference between an int, a long, a double and a decimal in Java? - Quora
Answer (1 of 5): There are eight primitive datatypes supported by Java. Primitive datatypes are predefined by the language and named by a keyword. int * Int data type is a 32-bit signed two's complement integer. * Minimum value is - 2,147,483,648 (-2^31) * Maximum value is 2,147,483,647(incl...
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › datatypes.html
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
The Long class also contains methods ... unsigned long. float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need ...
🌐
LabEx
labex.io › tutorials › java-how-to-convert-a-long-to-a-double-in-java-413969
How to convert a Long to a double in Java | LabEx
Method 1 (Implicit conversion): Original long value: 1234567890 Converted double value: 1.23456789E9 Method 2 (Explicit casting): Original long value: 1234567890 Converted double value: 1.23456789E9 Method 3 (Using doubleValue()): Original Long ...
🌐
Coderanch
coderanch.com › t › 689430 › java › primitive-long-double-compatibility
primitive long and double compatibility (Beginning Java forum at Coderanch)
Please remember that a double can handle factions/decimal numbers for instance 3.141592654 as well as whole numbers. Where as long can not handle factions/decimal numbers, but only whole numbers. Given your example code x is a whole number of 1000. It then automatically gets converted to a ...
🌐
YouTube
youtube.com › watch
Double, Long and Float Java Tutorial #9 - YouTube
$1,500 OFF ANY Springboard Tech Bootcamps with my code ALEXLEE1500. See if you qualify for the JOB GUARANTEE! 👉 https://bit.ly/3HX970hHere are the exact dif...
Published   January 10, 2019
🌐
TestMu AI Community
community.testmuai.com › ask a question
How does serialization performance compare between `long` and `double` in Java? - TestMu AI Community
April 1, 2025 - Both long and double take 8 bytes in Java, and while double offers a wider range for storing whole numbers, long might still be sufficient for many use cases. How does java long vs double serialization and deserialization performance compare? Also, does checking if a value is integer impact ...
🌐
Sololearn
sololearn.com › en › Discuss › 2059751 › whats-the-difference-between-float-double-and-long-variables
What's the difference between float , double and long variables? | Sololearn: Learn to code for FREE!
Long is similar to int but has 8 bytes of storage and cannot have decimal point values, whereas float is of 4 bytes and can store decimal point values but with lower precision. But double on the other hand has 8 bytes of storage and can as well store decimal point values with higher precision...
🌐
W3Schools
w3schools.com › java › java_data_types.asp
Java Data Types
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › Float-vs-Double-Whats-the-difference
Java double vs float: What's the difference?
If all of the values fall within the range of a float, it makes sense to use the smaller Java data type in this situation, otherwise the array elements needlessly consume memory. Data precision can be a problem with double and float types because binary numbers don’t always convert cleanly to decimals. For example, the following program prints out 11.3999 as the sum of 5.6 + 5.8:
🌐
Domo
community-forums.domo.com › home › community forums › archive
What the difference between double and long data types - Domo Community Forum
August 10, 2016 - I assume they are both floating point numbers?, but would like to know the capacities of each.