Possible Duplicate:
Integer with leading zeroes
I'm very new to Java. I did this :
System.out.println(01111);
which prints 4680
. Why it didnt print out 01111
?
Thanks in advance.
Possible Duplicate:
Integer with leading zeroes
I'm very new to Java. I did this :
System.out.println(01111);
which prints 4680
. Why it didnt print out 01111
?
Thanks in advance.
If you want to print out the string "01111" then put it in quotes. That's how you specify a string of characters in Java.
There is no decimal number 01111, so trying to print out the decimal number 01111 can't possibly work.
The reason you got 4680 is because, in Java source code, a leading zero before a numeric constant indicates that the number is specified in octal and numbers are printed out in decimal. 11110 octal = 4680 decimal. (You must have done 01110
to get 4680, 01111
would have given you 585.)
It prints 585, not 4680.
Integer literals that start with 0
are octal numerals. So 1111 is the octal representation of 585.
Preceding an integer with 0 means that it's an octal number literal, so your number is 1*8^3+1*8^2+1*8^1+1*8^0
.
Because the 0 prefix means an octal integer (base 8). So, 1111 octal is 585 decimal.
This is an octal representation of the number you are trying to print. The output you should be getting is "585"
and not "4680"
.