Maximum Limit of Java Long Primitive Type

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; } } 
+84
java
Mar 19 '13 at 16:46
source share
4 answers

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.

+263
Mar 19 '13 at 16:53
source share

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.

+37
Mar 19 '13 at 16:47
source share

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?

+8
Mar 19 '13 at 16:48
source share

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. 
+7
Mar 19 '13 at 16:47
source share



All Articles