This question already has an answer here:
这个问题已经有了答案:
- How does Java handle integer underflows and overflows and how would you check for it? 12 answers
- Java如何处理整数欠流和溢出,以及如何检查它?12个答案
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:
当我的“generateNumber”方法调用时,我使用的是长原始类型。如果朗达到他的最大极限会发生什么?是否将抛出任何异常或将重置为最小值?这是我的示例代码:
class LongTest {
private static long increment;
public static long generateNumber(){
++increment;
return increment;
}
}
4 个解决方案
#1
299
Long.MAX_VALUE
is 9,223,372,036,854,775,807
.
长。MAX_VALUE是9223372036854775807。
If you were executing your function once per nanosecond, it would still take over 292 years to encounter this situation according to this source.
如果你每纳秒执行一次你的函数,根据这个来源,你仍然需要超过292年的时间来遇到这种情况。
When that happens, it'll just wrap around to Long.MIN_VALUE
, or -9,223,372,036,854,775,808
as others have said.
当这种情况发生的时候,它就会缠绕很久。MIN_VALUE,或-9,223,372,036,854,775,808,正如其他人所说。
#2
40
It will overflow and wrap around to Long.MIN_VALUE
.
它将溢出并绕到Long.MIN_VALUE。
Its not too likely though. Even if you increment 1,000,000 times per second it will take about 300,000 years to overflow.
不过也不太可能。即使你每秒增加100万次,也需要30万年才能溢出。
#3
8
Exceding the maximum value of a long doesnt throw an exception, instead it cicles back. If you do this:
越过一个长值的最大值不会抛出异常,相反它会返回。如果你这样做:
Long.MAX_VALUE + 1
you will notice that the result is the equivalent to Long.MIN_VALUE.
您将注意到结果等价于Long.MIN_VALUE。
From here: java number exceeds long.max_value - how to detect?
从这里开始:java数字超过了长。max_value -如何检测?
#4
7
Ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.
范围从-9,223,372,036,854,775,808到+9,223,372,036,854,775,807。
It will start from -9,223,372,036,854,775,808
从-9,223,372,036,854,775,808开始
Long.MIN_VALUE.
#1
299
Long.MAX_VALUE
is 9,223,372,036,854,775,807
.
长。MAX_VALUE是9223372036854775807。
If you were executing your function once per nanosecond, it would still take over 292 years to encounter this situation according to this source.
如果你每纳秒执行一次你的函数,根据这个来源,你仍然需要超过292年的时间来遇到这种情况。
When that happens, it'll just wrap around to Long.MIN_VALUE
, or -9,223,372,036,854,775,808
as others have said.
当这种情况发生的时候,它就会缠绕很久。MIN_VALUE,或-9,223,372,036,854,775,808,正如其他人所说。
#2
40
It will overflow and wrap around to Long.MIN_VALUE
.
它将溢出并绕到Long.MIN_VALUE。
Its not too likely though. Even if you increment 1,000,000 times per second it will take about 300,000 years to overflow.
不过也不太可能。即使你每秒增加100万次,也需要30万年才能溢出。
#3
8
Exceding the maximum value of a long doesnt throw an exception, instead it cicles back. If you do this:
越过一个长值的最大值不会抛出异常,相反它会返回。如果你这样做:
Long.MAX_VALUE + 1
you will notice that the result is the equivalent to Long.MIN_VALUE.
您将注意到结果等价于Long.MIN_VALUE。
From here: java number exceeds long.max_value - how to detect?
从这里开始:java数字超过了长。max_value -如何检测?
#4
7
Ranges from -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.
范围从-9,223,372,036,854,775,808到+9,223,372,036,854,775,807。
It will start from -9,223,372,036,854,775,808
从-9,223,372,036,854,775,808开始
Long.MIN_VALUE.