Ambiguous Octal Literal
ID |
java.octal_values |
Severity |
critical |
Remediation Complexity |
trivial |
Remediation Risk |
medium |
Remediation Effort |
low |
Resource |
Code Smell |
Language |
Java |
Tags |
code-style |
Description
Reports int and long literals written with a leading zero. The Java compiler reads such a literal as octal (base 8), not decimal, so its value rarely matches what the digits suggest at a glance.
Rationale
A literal such as 010 does not hold ten — it holds eight, because the leading zero switches the literal to octal notation. Few developers reach for octal on purpose, so most readers — and most authors, later — misread the digits as a decimal value. The mistake is silent: the code compiles and runs, just with the wrong number.
// Bad -- looks decimal, is octal
int retryDelay = 010; // holds 8, not 10
int filePermissions = 0770; // holds 504, not 770
Remediation
Drop the leading zero to keep the intended decimal value, or switch to an unambiguous notation (hexadecimal 0x, binary 0b) when a non-decimal base really is intended.
// Good -- decimal value is explicit
int retryDelay = 10;
// Good -- non-decimal base is explicit and unambiguous
int filePermissions = 0b111111000;