This rule raises an issue when an integer literal starts with a zero followed by other digits, such as 010 or 060. Java
interprets such a literal as a base-8 octal value rather than a base-10 decimal value.
In Java, integer literals that begin with a leading zero are interpreted as octal numbers, not decimal numbers. This is a legacy feature inherited from C.
For example:
010 represents 8 in decimal, computed as 1×8 + 0060 represents 48 in decimal, computed as 6×8 + 001000 represents 512 in decimal, computed as 1×512 + 0×64 + 0×8 + 0This syntax is counterintuitive: most developers expect a literal such as 060 to mean sixty, with the leading zero being mere padding.
There is no visual cue that octal interpretation is happening, and code reviewers can easily miss the leading zero.
Octal literals are rarely needed in modern code. When a value is better expressed in another base, Java provides explicit prefixes: 0x
for hexadecimal and 0b for binary.
A common legitimate use of octal literals is Unix file permission modes, such as 0644 or 0755. Even there, the symbolic
notation accepted by java.nio.file.attribute.PosixFilePermissions.fromString, such as "rw-r—r--" or
"rwxr-xr-x", is easier to read and harder to get wrong.
For an integer literal whose complete source text matches 0[4-7][04-7]{2}, the rule message suggests the equivalent symbolic
permission string. This means exactly four characters: a leading zero, an owner digit from 4 to 7, and group and others digits each equal to 0, 4, 5,
6 or 7. Literals such as 04755, 0311 or 06_44 do not match this criterion and receive the general octal message
instead. Permission-mode literals are still reported, not exempted. The dedicated message also mentions the decimal value as an alternative, since
such a literal may be a zero-padded decimal like a time of day 0700.
This rule does not raise an issue for:
00 or 07. Their octal and decimal values are identical, so there is no
risk of confusion.0_ prefix, such as 0_7, 0_13 or 0_755, which makes the
intent explicit.int zero = 00; // Compliant: same value in octal and decimal int seven = 07; // Compliant: same value in octal and decimal int explicitOctal = 0_13; // Compliant: explicit octal intent int mode = 0_755; // Compliant: explicit octal intent
Using an octal literal when a decimal value is intended leads to incorrect calculations and logic errors. For example, setting a timeout to
060 seconds results in a 48-second timeout instead of the expected 60 seconds.
Such bugs are difficult to diagnose because the code compiles without any warning, and the discrepancy between the expected and actual values may only show up under specific conditions.
Use the decimal value instead. For file permission modes, prefer the symbolic permission string. If an octal value is really intended, use the
0_ prefix, which makes it clear to readers that the literal is octal.
int myNumber = 010; // Noncompliant: myNumber holds 8, not 10 int timeout = 060; // Noncompliant: timeout holds 48, not 60 int count = 01000; // Noncompliant: count holds 512, not 1000
int myNumber = 10; int timeout = 60; int count = 1000;
int mode = 0640; // Noncompliant: if this is a permission mode, use "rw-r-----" instead
Set<PosixFilePermission> mode = PosixFilePermissions.fromString("rw-r-----");
Or, if an octal value is intended, such as a file permission mode passed to a native API:
int mode = 0_640;