- 1 1. What Is the Maximum Value of Java’s long Type?
- 2 2. How to Get the Maximum long Value in Java
- 3 3. What Happens If a long Exceeds the Maximum Value? (Overflow)
- 4 4. Comparing Maximum Values with Other Integer Types
- 5 5. Practical Scenarios for Handling the Maximum long Value
- 6 6. Common Misconceptions About the long Maximum Value
- 7 7. Summary (Quick Review)
- 8 FAQ
- 8.1 Q1. What is the maximum value of Java’s long?
- 8.2 Q2. How many bits is a long?
- 8.3 Q3. What happens if you exceed Long.MAX_VALUE?
- 8.4 Q4. Does Java have unsigned long?
- 8.5 Q5. Is there a safe way to calculate the maximum value?
- 8.6 Q6. Should I use int or long?
- 8.7 Q7. How many digits can long handle?
- 8.8 Q8. What is the difference between long and BigInteger?
1. What Is the Maximum Value of Java’s long Type?
The maximum value of Java’s long type is 9223372036854775807.
It is defined as the constant Long.MAX_VALUE.
First, let’s confirm the conclusion.
public class Main {
public static void main(String[] args) {
System.out.println(Long.MAX_VALUE);
}
}Output:
9223372036854775807In other words, this is the largest positive value you can store in a Java long.
1.1 The long Maximum Value Is “9223372036854775807”
This value can be expressed as the following formula:
2^63 - 1The key point is why it’s “63”.
- The
longtype is a 64-bit integer. - 1 bit is used for the sign (positive/negative).
- The remaining 63 bits represent the numeric value.
Therefore,
maximum value = 2^63 - 1becomes the maximum.
*A “bit” is the smallest unit of data that represents 0 or 1.
1.2 Why Isn’t It 2^64 − 1 Even Though It’s 64-bit?
This is the point that confuses beginners the most.
Java’s long is a signed integer.
“Signed” means it can represent both positive and negative values.
Breakdown of 64 bits:
- 1 bit → sign
- 63 bits → value
So the largest positive value is:
2^63 - 1That’s why.
“2^64 – 1” applies to an unsigned integer.
Java does not have an unsigned long type.
⚠ Common misunderstanding
- × The long maximum value is 18446744073709551615
- ○ Correct: 9223372036854775807
1.3 Also Check the long Minimum Value
The minimum value is another must-know item to memorize along with the maximum.
System.out.println(Long.MIN_VALUE);Output:
-9223372036854775808In formula form:
-2^63The absolute value is 1 larger than the maximum because Java manages integers using two’s complement.
*Two’s complement is the standard mechanism computers use to represent negative numbers.
Common Pitfalls and Notes
- Mistaking it for
2^64-1 - Assuming an unsigned long exists
- Trying to memorize the value directly and getting the digits wrong
- Confusing it with int (32-bit)
In particular, confusing it with the int maximum value (2147483647) is extremely common.
2. How to Get the Maximum long Value in Java
When dealing with the maximum value of long, the rule is: don’t write the number directly—use the constant.
Here we’ll explain safe and practical ways to obtain it.
2.1 Use Long.MAX_VALUE (Recommended)
In Java, the maximum value is defined in the java.lang.Long class.
long max = Long.MAX_VALUE;
System.out.println(max);Output:
9223372036854775807Why should you use a constant?
- Prevents digit/typo mistakes
- Improves readability
- Makes the type explicit
- More robust to future changes (spec changes are unlikely, but it’s safer)
No special import is required.
The java.lang package is automatically imported.
2.2 Notes When Writing the Number Directly
You can also write the maximum value directly.
long max = 9223372036854775807L;The important part here is the trailing “L”.
Why is “L” required?
In Java, an integer literal (a numeric literal) is treated as an int by default if nothing is appended.
That means:
long max = 9223372036854775807; // ErrorThis causes a compile-time error.
Reason:
- It exceeds the range of int
- It does not explicitly indicate it is a long
Correct:
long max = 9223372036854775807L;⚠ A lowercase l is hard to distinguish from the digit 1, so uppercase L is recommended.
2.3 Risks of Hardcoding
“Hardcoding” means writing the numeric value directly.
Example:
if (value == 9223372036854775807L) {
// Processing
}Problems:
- The meaning is unclear
- Low maintainability
- Hard for other developers to understand
Recommended:
if (value == Long.MAX_VALUE) {
// Processing
}This makes the intent of the code clear.
Common Mistakes
- Forgetting to add
Land getting a compile error - Trying to assign it to an
intvariable - Writing
Long max = Long.MAX_VALUE;and not considering potential null handling (when using wrapper classes) - Trying to compute it with
Math.pow(2,63)(becomes double and introduces precision issues)
*Math.pow() returns a double, so it is not suitable for exact integer calculations.
3. What Happens If a long Exceeds the Maximum Value? (Overflow)
The long type can only handle a finite range.
So if you exceed the maximum value, it doesn’t throw an error—its value wraps around (cycles).
This is called “overflow.”
3.1 A Real Overflow Example
Let’s verify it in practice.
public class Main {
public static void main(String[] args) {
long max = Long.MAX_VALUE;
long overflow = max + 1;
System.out.println("Max value: " + max);
System.out.println("Max value + 1: " + overflow);
}
}Output:
Max value: 9223372036854775807
Max value + 1: -9223372036854775808You can see that max + 1 becomes the minimum value.
This is not abnormal—this is the behavior defined by the Java specification.
3.2 Why Doesn’t It Become an Error?
Java integer types (int / long) do not throw exceptions when the value exceeds the range.
Reason:
- Fixed number of bits (long is 64-bit)
- Values wrap around due to two’s complement representation
Conceptually:
Max value → +1 → Min value
Min value → -1 → Max valueThe computer simply computes bits, so it does not have the concept of “out of range.”
⚠ Common beginner confusion
- Assuming “it will be an error”
- The sign suddenly flips and becomes a bug
3.3 How to Detect Overflow
You can’t detect it with normal addition.
Method 1: Use Math.addExact() (Recommended)
try {
long result = Math.addExact(Long.MAX_VALUE, 1);
} catch (ArithmeticException e) {
System.out.println("Overflow occurred");
}Math.addExact() throws an ArithmeticException when the result exceeds the range.
There are also:
Math.subtractExact()Math.multiplyExact()
as well.
3.4 How to Do a Range Check
You can also check before adding.
if (value > Long.MAX_VALUE - addValue) {
System.out.println("Adding will overflow");
}*In practice, addExact() is usually safer than this approach.
3.5 Use BigInteger (No Upper Limit)
If long is not enough, use BigInteger.
import java.math.BigInteger;
BigInteger a = new BigInteger("9223372036854775807");
BigInteger b = BigInteger.ONE;
BigInteger result = a.add(b);
System.out.println(result);BigInteger has no digit limit.
But note:
- Slower than long
- Uses more memory
- Not a primitive type (it’s an object type)
Common Mistakes and Notes
- Using long for monetary calculations and not noticing overflow
- Generating IDs without considering upper limits
- Using Math.pow and introducing precision errors
- Converting to double and back (precision loss)
This can become a fatal bug, especially in financial processing.
4. Comparing Maximum Values with Other Integer Types
To understand the maximum value of long correctly, you should also organize how it differs from other integer types.
In Java, each integer type has a clearly defined range based on its bit width.
4.1 Difference from int
int is a 32-bit signed integer.
System.out.println(Integer.MAX_VALUE);Output:
2147483647Comparison:
| Type | Bit width | Maximum value |
|---|---|---|
| int | 32-bit | 2,147,483,647 |
| long | 64-bit | 9,223,372,036,854,775,807 |
A long can handle a range about 4.3 billion times larger than an int.
⚠ Common mistakes
- Assuming int is enough and later causing overflow due to growth
- Mapping a database BIGINT to int
4.2 Comparison with short and byte
Let’s also check smaller integer types.
System.out.println(Short.MAX_VALUE); // 32767
System.out.println(Byte.MAX_VALUE); // 127| Type | Bit width | Maximum value |
|---|---|---|
| byte | 8-bit | 127 |
| short | 16-bit | 32767 |
| int | 32-bit | 2147483647 |
| long | 64-bit | 9223372036854775807 |
Typical use cases:
- byte → data compression use cases
- short → small-scale numeric values
- int → typical integer processing
- long → large IDs and timestamps
4.3 When Should You Use long?
Typical cases where you use long:
- UNIX timestamps (milliseconds)
- Database BIGINT columns
- Large sequential IDs
- File sizes (bytes)
Example:
long timestamp = System.currentTimeMillis();System.currentTimeMillis() returns a long.
This is because millisecond-level values do not fit in an int.
4.4 Risks of Using long Unnecessarily
A long uses 8 bytes.
An int uses 4 bytes.
When handling large amounts of data:
- Memory usage increases
- Cache efficiency decreases
- Performance impact (depends on the environment)
Therefore,
If the range clearly fits in int, use int.
is the basic principle.
Common Mistakes and Notes
- The DB is BIGINT but Java uses int
- Receiving JSON numeric values as int even though they assume long
- Precision loss due to implicit casts during type conversion
- Assigning Math.pow results directly to long
Bugs due to type mismatches are especially common in API integrations.
5. Practical Scenarios for Handling the Maximum long Value
The maximum value of long is not just theoretical knowledge—it is important in real-world development.
Understanding it is essential, especially in processes where numeric overflow can become fatal.
5.1 UNIX Timestamps
When retrieving the current time in Java:
long now = System.currentTimeMillis();
System.out.println(now);System.currentTimeMillis() returns the number of milliseconds since January 1, 1970.
If you store it in an int:
int now = (int) System.currentTimeMillis(); // DangerousThe value is corrupted (only the lower 32 bits are kept).
⚠ Notes
- Truncation due to casting
- The Year 2038 problem (limit of int-based timestamps)
- Confusing seconds with milliseconds
Using long avoids the Year 2038 problem.

5.2 Integration with Databases (BIGINT)
In many databases, BIGINT is a 64-bit integer.
Example:
CREATE TABLE users (
id BIGINT PRIMARY KEY
);On the Java side:
long id;If you receive it as an int:
- Data corruption
- Exceptions when reaching the upper limit
- No future scalability
⚠ Common real-world mistakes
- int is sufficient at first → insufficient digits later
- Incorrect type mapping in ORM
- Numeric precision issues during JSON conversion (JavaScript is safe only up to 53 bits)
5.3 ID Generation and Upper Limits
Distributed IDs (e.g., Snowflake-style IDs) typically use long.
Example:
long id = generateId();Reasons:
- 64-bit space is sufficiently large
- Sequence, timestamp, and machine ID can be split into bits
However, theoretically, long also has an upper limit.
Example check:
if (currentId == Long.MAX_VALUE) {
throw new IllegalStateException("ID upper limit reached");
}In practice, this is rarely reached, but it should be considered in system design.
5.4 File Size Calculations
Getting a file size:
File file = new File("example.txt");
long size = file.length();For large files, int is not sufficient.
⚠ Notes
- Do not convert long to int
- Array indices are int (this is where limitations occur)
Common Real-World Mistakes
- Precision loss in JavaScript when handling long in JSON
- Not updating Java code when changing DB column types
- Using long for monetary calculations and causing overflow
- Designing persistent IDs without considering upper limits
6. Common Misconceptions About the long Maximum Value
The maximum value of long is a topic with many misunderstandings in search results.
Here we organize the most confusing points.
6.1 There Is No unsigned long in Java
This is especially confusing for developers with C/C++ experience.
Java does not have:
an unsigned long typeA long is always a 64-bit signed integer.
Therefore, the maximum positive value is:
2^63 - 1That’s the limit.
Since Java 8, the following methods were added:
Long.compareUnsigned(a, b);
Long.divideUnsigned(a, b);However, these are helper methods for unsigned operations—there is still no unsigned long type.
6.2 The Maximum Value Is Not 2^64 − 1
Misinformation often found online:
18446744073709551615This is the maximum value of an unsigned 64-bit integer.
Since Java’s long is signed:
The maximum value is 9223372036854775807That’s the correct value.
Summary of the difference:
| Type | Maximum value |
|---|---|
| Signed 64-bit | 2^63 – 1 |
| Unsigned 64-bit | 2^64 – 1 |
Java uses the former.
6.3 BigInteger and long Are Different
BigInteger represents integers with virtually no upper limit.
Difference:
| Type | Upper limit | Nature |
|---|---|---|
| long | Fixed 64-bit | Primitive type |
| BigInteger | Virtually unlimited | Object type |
BigInteger:
- Uses more memory
- Slower calculations
- Requires equals() for comparison
long:
- Fast
- Lightweight
- Fixed range
Choose according to your use case.
6.4 It’s Dangerous to Calculate the Maximum with Math.pow
Incorrect example:
long max = (long) Math.pow(2, 63) - 1;Problems:
- Math.pow returns a double
- double has 53-bit precision
- Precision errors occur for large integers
Correct approach:
long max = Long.MAX_VALUE;Summary of Common Confusions
- Thinking unsigned long exists
- Mistaking it for 2^64 – 1
- Trying to calculate it with Math.pow
- Destroying precision via double conversion
- Confusing BigInteger with long
7. Summary (Quick Review)
Here is a concise summary of the key points you need in practice.
7.1 Final Answer: long Maximum Value
- Maximum value: 9223372036854775807
- Constant: Long.MAX_VALUE
- Formula: 2^63 – 1
- Type: 64-bit signed integer
Verification code:
System.out.println(Long.MAX_VALUE);7.2 Essential Rules You Must Follow
- Do not hardcode the numeric maximum—use
Long.MAX_VALUE - Add
Lwhen writing a long literal - Do not confuse it with int
- Overflow does not throw an exception automatically
7.3 How to Prevent Overflow
To add safely:
Math.addExact(a, b);If the value may exceed the limit:
- Use BigInteger
- Implement range checks
7.4 Important Points in Practice
- Receive DB BIGINT values as long
- Use long for UNIX timestamps
- Be careful with precision in JSON integrations
- Consider upper limits when designing ID systems
7.5 Most Important Checklist
- Are you mistakenly using 2^64 – 1?
- Do you assume unsigned long exists?
- Are you calculating it with Math.pow?
- Are you casting long to int?
- Do you understand what happens on overflow?
The maximum value of long is not just something to memorize—it is a core concept that affects your understanding of integer types as a whole.
FAQ
Q1. What is the maximum value of Java’s long?
9223372036854775807.
You can retrieve it with Long.MAX_VALUE.
Q2. How many bits is a long?
It is a 64-bit signed integer.
1 bit is for the sign, and the remaining 63 bits represent the value.
Q3. What happens if you exceed Long.MAX_VALUE?
No error occurs.
It wraps around to the minimum value (-9223372036854775808).
System.out.println(Long.MAX_VALUE + 1);Q4. Does Java have unsigned long?
No, not as a type.
Java 8 and later provide helper methods for unsigned operations, but the type itself is always signed.
Q5. Is there a safe way to calculate the maximum value?
Do not calculate it directly. Always use:
Long.MAX_VALUEMath.pow() returns a double and is therefore inaccurate for large integers.
Q6. Should I use int or long?
- If the range is within about 2.1 billion → use int
- If it may exceed that → use long
Use long for DB BIGINT columns and timestamps.
Q7. How many digits can long handle?
The maximum value has 19 digits:9223372036854775807
Q8. What is the difference between long and BigInteger?
- long → fixed 64-bit, fast
- BigInteger → virtually unlimited, slower
If the range fits within long, using long is the standard approach.


