java 整数太大
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17632914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Integer number too large
提问by Strife
Hi I'm having trouble understanding why this isn't working
嗨,我无法理解为什么这不起作用
if(Long.parseLong(morse) == 4545454545){
System.out.println("2");
}
Where morse is just a String of numbers. The problem is it says Integer number too large: 4545454545, but I'm sure a Long can be much longer than that.
其中 morse 只是一串数字。问题是它说整数太大:4545454545,但我确信 Long 可能比这长得多。
回答by NINCOMPOOP
You need to use 4545454545lor 4545454545Lto qualify it as long. Be default , 4545454545is an intliteral and 4545454545is out of range of int.
您需要使用4545454545l或4545454545L将其限定为long. 默认情况下,4545454545是int文字并且4545454545超出范围int。
It is recommended to use uppercase alphabet Lto avoid confusion , as land 1looks pretty similar
建议使用大写字母L以避免混淆,因为l和1看起来非常相似
You can do :
你可以做 :
if(Long.valueOf(4545454545l).equals(Long.parseLong(morse)) ){
System.out.println("2");
}
OR
或者
if(Long.parseLong(morse) == 4545454545l){
System.out.println("2");
}
As per JLS 3.10.1:
根据JLS 3.10.1:
An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise it is of type int (§4.2.1).
如果整数文字以 ASCII 字母 L 或 l (ell)为后缀,则它是 long 类型;否则它是 int 类型(第 4.2.1 节)。
回答by Michael Berry
If your integer value is larger than 2147483647, as your literal is then you need to use a long literal:
如果您的整数值大于2147483647,就像您的文字一样,那么您需要使用长文字:
4545454545L
4545454545L
...note the Lat the end, which is the difference between a long and an int literal. A lower case lworks too, but is less readable as it's easily confused with a 1 (not a great thing when you're dealing with a number!)
...注意L最后的,这是 long 和 int 文字之间的区别。小写l也可以,但可读性较差,因为它很容易与 1 混淆(当您处理数字时,这不是一件好事!)
回答by nachokk
You need to use 4545454545Lor 4545454545lto qualify it as long.
您需要尽可能长时间地使用4545454545L或 4545454545l限定它。

