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

Why Are Integer Literals Like 08 Invalid in Java?

Susan Sarandon
Release: 2024-12-04 09:22:15
Original
690 people have browsed it

Why Are Integer Literals Like 08 Invalid in Java?

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:

  • 010 represents 8 in octal, not 10 in decimal
  • 024 represents 20 in octal, not 24 in decimal

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template