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.

Discussions

What the difference between double and long data types
I assume they are both floating point numbers?, but would like to know the capacities of each. More on community-forums.domo.com
๐ŸŒ community-forums.domo.com
August 11, 2016
How does serialization performance compare between `long` and `double` in Java? - TestMu AI Community
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 ... More on community.testmuai.com
๐ŸŒ community.testmuai.com
0
April 1, 2025
java - Confused with size of long and double - Stack Overflow
Looking at Java (but probably similar or the same in other languages), a long and a double both use 8 bytes to store a value. A long uses 8 bytes to store long integers from -9,223,372,036,854,775... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Which is larger? Long vs Double
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
๐ŸŒ r/javahelp
10
3
January 19, 2023
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_data_types.asp
Java Data Types
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
๐ŸŒ
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 ...
๐ŸŒ
Domo
community-forums.domo.com โ€บ home โ€บ community forums โ€บ archive
What the difference between double and long data types - Domo Community Forum
August 11, 2016 - I assume they are both floating point numbers?, but would like to know the capacities of each.
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2059751 โ€บ what-s-the-difference-between-float-double-and-long-variables
What's the difference between float , double and long variables? | Sololearn: Learn to code for FREE!
November 5, 2019 - I know an integer can hold a whole number, a float can hold a decimal number and an unsigned can hold only positive numbers. But what is the function of double and long
๐ŸŒ
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
๐ŸŒ
RETIT
retit.de โ€บ performance-in-detail-long-vs-double-in-java-2
Performance in Detail: Long vs. Double in Java โ€“ RETIT
May 8, 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 to List , we noticed some severe performance issues. Our application took minutes to process data it previously handled in seconds.
๐ŸŒ
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?
What's the difference between double vs float data types? In this quick tutorial we show how float and double Java types differ, along with guidance on how to choose one over the other.
๐ŸŒ
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 ...
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 328469 โ€บ java โ€บ comparision-double-long
comparision between double and long (Java in General forum at Coderanch)
July 21, 2006 - Why 1234567891.00000001 == (long)1234567891.00000001 returns true even though they are fals. Is that possible?
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-data-types
Java Data Types - GeeksforGeeks
Your All-in-One Learning Portal. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.
Published ย  January 16, 2026
๐ŸŒ
Quora
quora.com โ€บ What-is-the-difference-between-using-double-for-floating-point-numbers-in-Java-as-opposed-to-using-float-or-long-in-C-C
What is the difference between using double for floating point numbers in Java as opposed to using float or long in C/C++? - Quora
Answer (1 of 2): Disclaimer: I am not a Java expert Question is sort of confusing. A long is an integral type, so I think we can ignore that part of the question. Computer languages provide some level of abstraction over the CPU. But that doesnโ€™t mean they abandon the capabilities of the CPU an...
๐ŸŒ
Quora
quora.com โ€บ What-is-the-difference-between-double-long-float-and-int-variables-in-Java-Which-one-should-be-used-for-storing-large-numbers-more-than-two-digits-Why
What is the difference between double, long, float, and int variables in Java? Which one should be used for storing large numbers (more than two digits)? Why? - Quora
Answer: In Java, int and long are primitive integer datatypes. They store integers as twoโ€˜s complement numbers. Ints use 32 bits and have a range from -2^{31}=-2147483648 to 2^{31}-1=2147483647, longs use 64 bits and have a range from -2^{63}=-9223372036854775808 to 2^{63}-1=9223372036854775807.
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 โ€บ Why-does-Java-use-double-for-floating-point-numbers-instead-of-long-for-scientific-calculations
Why does Java use double for floating point numbers instead of long for scientific calculations? - Quora
Answer: Usage of floating point arithmetics in scientific calculations is not related to Java. Floating points are able to represent real number with varying precision for different magnitudes: number with smaller magnitudes are represented with higher precision and numbers with large magnitudes...
๐ŸŒ
Swan
pyweb.swan.ac.uk โ€บ ~allton โ€บ VisualBasic โ€บ node9.html
9. Declaring your Variable Types: Long, Double and String, ...
Long is for integer numbers. Double is for real numbers (i.e. numbers which have decimal points in them!).
๐ŸŒ
Quora
quora.com โ€บ What-is-the-difference-between-float-double-and-long-double-in-the-programming-language-C
What is the difference between float, double and long double in the programming language C? - Quora
Answer (1 of 2): The main difference is the amount of storage allocated. Generally you need more storage for a double than a float, and more for a long double and a double. How much this allocated storage is, is implementation dependent. As you can see, in the implementation that I use, there is...
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ java-long
The Long Data Type in Java: A Detailed How-To Guide
February 26, 2024 - Are you finding it challenging to handle large numerical values in Java? You're not alone. Many developers grapple with this task, but there's a data type in
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ float vs double in java - difference you should know
Float Vs Double in Java - Difference You Should Know - Scaler Topics
March 27, 2024 - Learn about the difference between Float and Double on Scaler Topics along with their key differences and a comparison chart.