Java Long原始类型最大限制 [英] Java Long primitive type maximum limit

查看:208
本文介绍了Java Long原始类型最大限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Long基元类型,只要我的'generateNumber'method调用,它就会递增1。如果Long达到他的最大限制会怎么样?会抛出任何异常还是会重置为最小值?
这是我的示例代码:

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 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.

当发生这种情况时,它只会回绕到 Long.MIN_VALUE ,或 -9,223,372,036,854,775,808 正如其他人所说。

When that happens, it'll just wrap around to Long.MIN_VALUE, or -9,223,372,036,854,775,808 as others have said.

这篇关于Java Long原始类型最大限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆