I have been trying to investigate why Integer.parseInt()
removes 0 and returns the remaining numbers. For example:
@Test
public void testInteger() {
System.out.println(Integer.parseInt("01234"));
System.out.println(Integer.parseInt("12340"));
}
The first line prints 1234 and the second 12340. Why is the 0 removed from the first line. This does not make sense. I am aware a Integer is the wrapper object of an int and an int stores 32 bits.
Could you please explain why?
Because you are using the string "01234" as an integer (with parseInt()
), the system.out.println
is printing an integer.
"01234" as an integer would be 1234. This is because 0 is the only integer that starts with a zero.
None of these are real numbers:
0456,
0234,
05555555,
01
If you were to store "01234" as a string, the 0 would hold its value.