-1

why the compiler is changing the value of int while conversion to string and how it gets it new value.

int n = 1003456;
String str = Integer.toString(n);
System.out.println(str.length() + " " + n + " " + str);
int m = 0013456;
String string = Integer.toString(m);
System.out.println(string.length() + " " + m + " " + string);

the output of the above program is :

7 1003456 1003456
4 5934 5934

the first line of the output is clear but the second line of output shows the size of integer is 4 (but i think it should be 5 if m is 13456).how the new value of m is changed. how should i manipulate the code to get the my value of m.

Lokesh Pandey
  • 1,739
  • 23
  • 50
Omkar Nath
  • 23
  • 4

1 Answers1

3

An integer literal that starts with 0 is interpreted by the compiler as an octal number, not as a decimal one. So

int m = 0013456;

is equivalent to

int m = 5934;
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255