Java parseInt vs parseLong

2019-09-18 18:02发布

问题:

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.