Home >Java >javaTutorial >Why Are 08 and 09 Invalid Integer Literals in Java?
Why 08 is Invalid as an Integer Literal in Java
In Java, integer literals starting with 0 are interpreted as octal numbers (base 8). This can lead to confusion for numbers with multiple digits.
When 0 is followed by a single digit (except 8 or 9), the value is simply the numerical value of that digit. However, for numbers with more than one digit, the interpretation can be surprising.
For instance:
However, 08 and 09 are invalid integer literals in Java. This is because there are only 8 digits (0-7) in the octal system, so any number beginning with 08 or 09 would be out of range.
As a best practice, it's recommended to never start an integer literal with 0, unless you specifically intend to write zero itself. This will help avoid unexpected results due to octal interpretation.
The above is the detailed content of Why Are 08 and 09 Invalid Integer Literals in Java?. For more information, please follow other related articles on the PHP Chinese website!