Java parseInt vs parseLong

半世苍凉 提交于 2019-12-11 19:00:52

问题


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?


回答1:


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.

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



来源:https://stackoverflow.com/questions/24590725/java-parseint-vs-parselong

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!