Java Integer Literals: Why is 08 Invalid?
In Java, integer literals written as a sequence of decimal digits represent a decimal (base 10) value. Conversely, if an integer literal begins with the digit 0, it is interpreted as an octal (base 8) quantity.
Exception for Small Integers
For single-digit numbers, the octal interpretation is identical to the decimal interpretation, except for the values 08 and 09. Therefore, you may not notice that these numbers are being treated as octal initially.
Confusion with Multi-Digit Numbers
However, if an integer literal has multiple digits, the octal interpretation can lead to unexpected results. For example:
Avoid Leading Zeros
To ensure clarity and avoid confusion, it is recommended to use a prefix for octal literals, such as 0o (e.g., 0o10), or to simply avoid beginning an integer literal with the digit 0 unless you intend to write zero itself.
The above is the detailed content of Why Are Integer Literals Like 08 Invalid in Java?. For more information, please follow other related articles on the PHP Chinese website!