This content has been marked as final. Show 2 replies
Welcome to the forum.
numeric literals starting with a '0' are treated als octal numbers.
hence <tt>010</tt> resolves to:
<pre> 0*8^2 + 1*8^1 + 0*8^0 = 8</pre>
Welcome to the forum!
when we store value like
int i = 010;
it will display the output of i as 8.
Why is it so??
It isn't just 'stored' as an 8 it IS an 8. It is displayed as decimal so, as an 8 it displays as decimal 8.
Your question is really why is 010 stored as an 8. The answer to that can be found in The Java Language Specification
Section 3.10.1 Integer Literals has your answer. You should read then entire section but this is the spec that applies
An octal numeral consists of an ASCII digit 0 followed by one or more of the ASCII digits 0 through 7 interspersed with underscores, and can represent a positive, zero, or negative integer.
0 Underscores OctalDigits
OctalDigit OctalDigitsAndUnderscoresopt OctalDigit
OctalDigit: one of
0 1 2 3 4 5 6 7
Note that octal numerals always consist of two or more digits; 0 is always considered to be a decimal numeral - not that it matters much in practice, for the numerals 0, 00, and 0x0 all represent exactly the same integer value.
Then, as TPD showed, octal 010 is decimal 8.