Home >Java >javaTutorial >Why Are 08 and 09 Invalid Integer Literals in Java?

Why Are 08 and 09 Invalid Integer Literals in Java?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-06 19:43:12952browse

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:

  • 010 is interpreted as 8 (1 x 8 0)
  • 024 is interpreted as 20 (2 x 8 4)

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn