Java parseInt 与 parseLong
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24590725/
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
Java parseInt vs parseLong
提问by aandis
String a = "576055795";
long b = 10*Integer.parseInt(a);
long c = 10*Long.parseLong(a);
System.out.println(b); //Prints 1465590654
System.out.println(c); // Prints 5760557950
Why are they different?
他们为什么不同?
采纳答案by user3553031
Integer.parseInt()
returns an int
, which is a signed 32-bit integer. 10
is also an int
; multiplying 576055795
by 10
as ints overflows and yields an int
, which is then promoted to a long
.
Integer.parseInt()
返回一个int
,它是一个有符号的 32 位整数。 10
也是一个int
; 乘以576055795
由10
作为整数溢出和产率的int
,然后将其提升到一个long
。
Long.parseLong()
returns a long
, which is a signed 64-bit integer. Multiplying it by 10
yields a long with no overflow.
Long.parseLong()
返回 a long
,它是一个有符号的 64 位整数。乘以它会10
产生一个没有溢出的 long。