I am using the Long primitive type which increments by 1 whenever my 'generateNumber'method called. What happens if Long reaches to his maximum limit? will throw any exception or will reset to minimum value?
here is my sample code:
class LongTest {
private static long increment;
public static long generateNumber(){
++increment;
return increment;
}
}
解决方案
Long.MAX_VALUE is 9,223,372,036,854,775,807.
If you were executing your function once per nanosecond, it would still take over 292 years to encounter this situation according to this source.
When that happens, it'll just wrap around to Long.MIN_VALUE, or -9,223,372,036,854,775,808 as others have said.