I use a long primitive type that increments by one each time the generateNumber method is called. What happens if Long reaches its maximum limit? will throw any exception or will it reset to the minimum value? here is my sample code:
class LongTest { private static long increment; public static long generateNumber(){ ++increment; return increment; } }
Long.MAX_VALUE 9,223,372,036,854,775,807 .
Long.MAX_VALUE
9,223,372,036,854,775,807
If you performed your function once in a nanosecond, this situation would have required more than 292 years according to this source .
When this happens, it will simply turn to Long.MIN_VALUE or -9,223,372,036,854,775,808 , as others have said.
Long.MIN_VALUE
-9,223,372,036,854,775,808
It will overflow and Long.MIN_VALUE up to Long.MIN_VALUE .
This is not too likely. Even if you increase 1,000,000 times per second, it will take about 300,000 years to overflow.
If the maximum value is exceeded, long does not throw an exception; instead, it throws back. If you do this:
Long.MAX_VALUE + 1
You will notice that the result is equivalent to Long.MIN_VALUE.
Hence: java number exceeds long.max_value - how to determine?
The range is from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.
It will start from -9,223,372,036,854,775,808
Long.MIN_VALUE.