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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:16:37  来源:igfitidea点击:

Java parseInt vs parseLong

javaintegerlong-integer

提问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. 10is also an int; multiplying 576055795by 10as ints overflows and yields an int, which is then promoted to a long.

Integer.parseInt()返回一个int,它是一个有符号的 32 位整数。 10也是一个int; 乘以57605579510作为整数溢出和产率的int,然后将其提升到一个long

Long.parseLong()returns a long, which is a signed 64-bit integer. Multiplying it by 10yields a long with no overflow.

Long.parseLong()返回 a long,它是一个有符号的 64 位整数。乘以它会10产生一个没有溢出的 long。