Long.MAX_VALUE in Java is 9223372036854775807.Long.MIN_VALUE in Java is -9223372036854775808.
These constants define the maximum and minimum values a 64-bit signed long data type can store in Java.
Best Practice: In your code, always use the constants Long.MAX_VALUE and Long.MIN_VALUE instead of hardcoding numbers to ensure readability and prevent typos.
Quick Answer: Java long Max and Min Value
| Constant | Numeric Value | Power of 2 |
Long.MAX_VALUE |
9,223,372,036,854,775,807 | 2⁶³ − 1 |
Long.MIN_VALUE |
-9,223,372,036,854,775,808 |
−2⁶³ |
In your code, always use the constants
Long.MAX_VALUEandLong.MIN_VALUEinstead of typing out the full number to ensure readability and prevent typos.
What is the long Data Type in Java?
In Java, long is a primitive data type used when you need a range of values wider than those provided by int.
- Size: 64 bits (8 bytes)
- Signed range: −2⁶³ to (2⁶³ − 1)
- Default value:
0L - The "L" Suffix: You must append an
L(e.g.,100L) to literals that exceed the 32-bitintrange, otherwise the code will fail to compile.
Because remembering such large numbers is impractical, Java provides the constants:
Long.MAX_VALUELong.MIN_VALUE
These constants are defined in the Long wrapper class and represent the maximum and minimum values a long can store.
long vs int (common confusion)
| Feature | int | long |
| Storage | 32-bit | 64-bit |
| Max Value | ~2.14 Billion | ~9.22 Quintillion |
| Use Case | General counting | Timestamps, Large IDs, Scientific data |
Why are these the limits? (The Math)
JJava uses two’s complement binary representation.
- 1 bit is for the sign (positive/negative).
- 63 bits store the actual value.
This is why the max value is 2⁶³ - 1 The 2⁶³ exists because zero is included in the positive range of bits.
Example: Print Long.MAX_VALUE and Long.MIN_VALUE
public class Main {
public static void main(String[] args) {
System.out.println(Long.MAX_VALUE);
System.out.println(Long.MIN_VALUE);
}
}
Output:
9223372036854775807
-9223372036854775808
Handling long Overflow and Underflow in Java
Java does not throw an exception when a long exceeds its maximum or minimum limit. Instead, the value wraps around the number line due to two’s complement arithmetic. This behavior is known as integer overflow or underflow.
For example, if you add 1 to Long.MAX_VALUE, it overflows and becomes Long.MIN_VALUE:
long val = Long.MAX_VALUE + 1;
System.out.println(val); // Output: -9223372036854775808
Similarly, subtracting 1 from Long.MIN_VALUE causes underflow and wraps back to Long.MAX_VALUE.
How to Detect and Prevent long Overflow in Java
Java allows long arithmetic to overflow silently, which can lead to incorrect results in critical applications. To handle this safely, Java provides built-in methods and alternative data types.
Using Math.addExact():
try {
long result = Math.addExact(Long.MAX_VALUE, 1);
} catch (ArithmeticException e) {
System.out.println("Overflow detected");
}
Math.addExact() throws an ArithmeticException when overflow occurs, making it ideal for detecting and preventing overflow at runtime.
For values beyond the 64-bit range, use BigInteger:
BigInteger big = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE);
Use BigInteger when numbers can exceed the 64-bit range and absolute precision is required.
Unsigned long values in Java (Java 8+)
This is an advanced topic and not commonly used in everyday Java applications.
Java does not have an unsigned long primitive. However, Java 8 introduced methods to treat long values as unsigned, with a range from 0 to 2^64 − 1.
Unsigned Max Value: $18,446,744,073,709,551,615$
Usage: Long.toUnsignedString(long i) or Long.compareUnsigned(x, y).
When should you NOT use long?
Avoid using long when:
- Storing IDs, account numbers, or phone numbers
- Precision matters beyond 64 bits
Use instead:
Stringfor identifiersBigIntegerfor extremely large numbers
Frequently Asked Questions (FAQ)
What is the max value of long in Java?
9223372036854775807
What is the min value of long in Java?
-9223372036854775808
Why does Long.MAX_VALUE + 1 become negative?
Because Java uses 64-bit two’s complement arithmetic and overflows wrap around.
Does Java throw an exception on long overflow?
No, unless you use methods like Math.addExact().
What happens if I don't add the 'L' suffix?
If you write long x = 100;, it works because 100 fits in an int. However, long x = 9223372036854775807; will cause a compilation error because Java treats literal whole numbers as int by default. You must use 9223372036854775807L.
Is Long.MAX_VALUE the same in all Java versions?
Yes. The size of primitives is architecturally independent in Java, meaning a long is always 64-bit on any operating system or JVM version.
How many digits is Java's long max value?
It is 19 digits long (9,223,372,036,854,775,807).
Does the long max value change on 32-bit systems?
No. Java is platform-independent. A long is strictly 64-bit on all JVM architectures.
Summary
Long.MAX_VALUE and Long.MIN_VALUE define the boundaries of the 64-bit long type. Because Java allows overflow to happen silently, always use Math.addExact() or BigInteger when precision and safety are paramount.
References


