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
🌐
Oracle
docs.oracle.com › javase › tutorial › java › nutsandbolts › datatypes.html
Primitive Data Types (The Java™ Tutorials > Learning the Java Language > Language Basics)
See the section The Number Classes for more information. Static methods like compareUnsigned, divideUnsigned etc have been added to the Integer class to support the arithmetic operations for unsigned integers. long: The long data type is a 64-bit two's complement integer.
🌐
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?
In Java, the Float and Double wrapper classes have two properties that return the upper and lower limits of float and double data types, MIN_VALUE and MAX_VALUE:
🌐
Coderanch
coderanch.com › t › 670713 › java › Double-MAX
What is Double.MAX_VALUE + 1? [Solved] (Java in General forum at Coderanch)
That means that no matter how small value2 becomes, as long as both operands are positive, you well never get POSITIVE_INFINITY, because MAX_VALUE is the closest approximation of the actual result. The matter is different when value2 is 0.0 or -0.0. I'm not certain if this is correct, but here's an overview of what I believe are the correct results of divisions when zero or infinity are involved: ... It shou‍ld all be in the Java® Language Specification (=JLS).
Top answer
1 of 3
45

Double.MAX_VALUE is the maximum value a double can represent (somewhere around 1.7*10^308).

This should end in some calculation problems, if you try to subtract the maximum possible value of a data type.

Even though when you are dealing with money you should never use floating point values especially while rounding this can cause problems (you will either have to much or less money in your system then).

2 of 3
16

Resurrecting the dead here, but just in case someone stumbles against this like myself. I know where to get the maximum value of a double, the (more) interesting part was to how did they get to that number.

double has 64 bits. The first one is reserved for the sign.

Next 11 represent the exponent (that is 1023 biased). It's just another way to represent the positive/negative values. If there are 11 bits then the max value is 1023.

Then there are 52 bits that hold the mantissa.

This is easily computed like this for example:

public static void main(String[] args) {

    String test = Strings.repeat("1", 52);

    double first = 0.5;
    double result = 0.0;
    for (char c : test.toCharArray()) {
        result += first;
        first = first / 2;
    }

    System.out.println(result); // close approximation of 1
    System.out.println(Math.pow(2, 1023) * (1 + result));
    System.out.println(Double.MAX_VALUE);

} 

You can also prove this in reverse order :

    String max = "0" + Long.toBinaryString(Double.doubleToLongBits(Double.MAX_VALUE));

    String sign = max.substring(0, 1);
    String exponent = max.substring(1, 12); // 11111111110
    String mantissa = max.substring(12, 64);

    System.out.println(sign); // 0 - positive
    System.out.println(exponent); // 2046 - 1023 = 1023
    System.out.println(mantissa); // 0.99999...8
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.

🌐
Delft Stack
delftstack.com › home › howto › java › double max value in java
How to Double max value in Java | Delft Stack
February 14, 2024 - Here, maxValue is a variable of type double, and it is assigned the maximum finite value that can be represented by a double. In Java, Double.MAX_VALUE is a constant representing the maximum finite positive value that can be represented by the double data type.
Find elsewhere
🌐
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 - On the other hand, double stores values from 1.7e-308 to 1.7e+038. Moreover, one other difference between long and double in Java is that long is an integral type while double is a floating point type.
🌐
MIT
web.mit.edu › java_v1.0.2 › www › javadoc › java.lang.Double.html
Class java.lang.Double
Returns the long value of this Double (by casting to a long). ... Returns a String representation of this Double object. ... Returns a String representation for the specified double value. ... Returns a new Double value initialized to the value represented by the specified String. ... Positive infinity. NEGATIVE_INFINITY ... Negative infinity. NaN ... Not-a-Number. Is not equal to anything, including itself. MAX_VALUE
🌐
Blogger
javahungry.blogspot.com › 2022 › 11 › long-vs-double.html
Java long vs double | Java Hungry
The value of long ends with "L" whereas the value of double ends with "d". For example, ... 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.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › Double.html
Double (Java Platform SE 7 )
Otherwise, s is regarded as ... to type double by the usual round-to-nearest rule of IEEE 754 floating-point arithmetic, which includes preserving the sign of a zero value. Note that the round-to-nearest rule also implies overflow and underflow behaviour; if the exact value of s is large enough in magnitude (greater than or equal to (MAX_VALUE + ...
🌐
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
Original Long value: 9223372036854775807 Converted to double: 9.223372036854776E18 Converted back to Long: 9223372036854775808 Are they equal? false Difference: -1 Million as Long: 1000000 Million as double: 1000000.0 Million as double (with ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Double.html
Double (Java Platform SE 8 )
October 20, 2025 - Otherwise, s is regarded as representing an exact decimal value in the usual "computerized scientific notation" or as an exact hexadecimal value; this exact numerical value is then conceptually converted to an "infinitely precise" binary value that is then rounded to type double by the usual round-to-nearest rule of IEEE 754 floating-point arithmetic, which includes preserving the sign of a zero value. Note that the round-to-nearest rule also implies overflow and underflow behaviour; if the exact value of s is large enough in magnitude (greater than or equal to (MAX_VALUE + ulp(MAX_VALUE)/2), rounding to double will result in an infinity and if the exact value of s is small enough in magnitude (less than or equal to MIN_VALUE/2), rounding to float will result in a zero.
🌐
Coderanch
coderanch.com › t › 689430 › java › primitive-long-double-compatibility
primitive long and double compatibility (Beginning Java forum at Coderanch)
If you go back to the JLS link ... and a long each occupy 64 bits, and the long can exactly define every whole number in its range. But the double has a much larger range, so it must miss out some values. I learnt about that first from the Deitel and Deitel Java5 ...