Overview
long is the largest integer data type in Java. It stores whole numbers in
8 bytes (64 bits) of memory, with a range of about ±9.22 × 1018.
Use it when values can go beyond the int limit of about 2.1 billion, for example with timestamps, file sizes, bank transaction IDs or population counts.
Java treats every whole-number literal as an int by default. Add L at the end to make it a long. Use a capital L, because a lowercase l looks like the digit 1.
Step-by-Step Explanation
1
Declare and Initialise long Variables
The L suffix marks a long literal.
long a = 100000L;
long b = -200000L;
These values are small enough to fit in an int, so the L is optional here. It's still good practice, because it shows clearly that the value is meant to be a long.
2
Print the Values
The suffix isn't printed.
System.out.println("a = " + a);
System.out.println("b = " + b);
The output is a = 100000 and b = -200000. The L is only part of the source code.
3
Range of long
The largest and smallest values.
System.out.println("Max long value = " + Long.MAX_VALUE);
System.out.println("Min long value = " + Long.MIN_VALUE);
Long is the wrapper class for long. With 64 bits, a long can store $$2^{64}$$ different values, from $$-2^{63}$$ to $$2^{63} - 1$$.
4
long + long Stays a long
No cast is needed.
long sum = a + b;
System.out.println("sum = " + sum);
Adding two long values gives a long, so the result -100000 is stored directly.
5
How long Fixes int Overflow
The type of the calculation matters.
int intResult = 50000 * 50000;
long longResult = 50000L * 50000;
The correct answer is $$50000 \times 50000 = 2{,}500{,}000{,}000$$, which is bigger than the int maximum. In the first line, both numbers are int, so the result overflows to -1794967296.
In the second line, 50000L is a long, so Java does the whole calculation as long and gets the right answer.
6
long Can Overflow Too
The limit is bigger, not unlimited.
long overflow = Long.MAX_VALUE + 1;
Going past the maximum wraps around to the minimum, without any error:
$$9223372036854775807 + 1 \rightarrow -9223372036854775808$$
For numbers bigger than this, use BigInteger.
7
Large Literals Need the L Suffix
Here the L is required.
long worldPopulation = 8_000_000_000L;
8 billion is above the int range. Without L, Java treats it as an int and gives the error integer number too large. The underscores only improve readability and don't appear in the output.
long at a Glance
| Property |
Value |
| Size |
8 bytes (64 bits) |
| Range |
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
| Default value (fields) |
0L |
| Literal suffix |
L (or l, not recommended) |
| Wrapper class |
Long |
Java Integer Types Compared
| Type |
Size |
Range |
Suffix |
byte |
1 byte |
-128 to 127 |
None |
short |
2 bytes |
-32,768 to 32,767 |
None |
int |
4 bytes |
-2,147,483,648 to 2,147,483,647 |
None |
long |
8 bytes |
About ±9.22 × 1018 |
L |
Output
a = 100000
b = -200000
Max long value = 9223372036854775807
Min long value = -9223372036854775808
sum = -100000
int result (50000 * 50000) = -1794967296
long result (50000L * 50000) = 2500000000
Long.MAX_VALUE + 1 = -9223372036854775808
worldPopulation = 8000000000
Common Mistakes
Missing L on a large literal
long x = 8000000000; gives the error integer number too large, even though the variable is a long.
Overflowing before assignment
long x = 50000 * 50000; still gives a wrong negative number. The multiplication happens in int first, and only then is the result stored in the long.
Using a lowercase l
100l is easy to misread as 1001. Always use a capital L.
Good practice
Put the L on the first number in a calculation, like 50000L * 50000, so the whole expression runs as long.
Key Takeaway
long stores whole numbers in 8 bytes, up to about ±9.22 × 1018.
Add L to long literals, and make sure the calculation itself runs as long, or it can overflow before the value is stored.