...
Do not use multiple identifiers that vary by only one or more visually similar characters. Also, make the initial portions of long identifiers distinct to aid recognition.
According to the JLS, §3.10.1, "Integer Literals" [JLS 2013],
An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise, it is of type int. The suffix L is preferred because the letter l (ell) is often hard to distinguish from the digit 1 (one).
Consequently, use L
, not l
, to clarify programmer intent when indicating that an integer literal is of type long
.
Integer literals with leading zeros, in actuality, denote octal values, not decimal values. According to §3.10.1, "Integer Literals" of the JLS [JLS 2013],
An octal numeral consists of an ASCII digit
0
followed by one or more of the ASCII digits0
through7
interspersed with underscores, and can represent a positive, zero, or negative integer.
This misinterpretation may result in programming errors and is more likely to occur while declaring multiple constants and trying to enhance the formatting with zero padding.
Noncompliant Code Example
...
This noncompliant example prints the result of adding an int
and a long
value even though it appears that two integers 11111
are being added:. According to the JLS, §3.10.1, "Integer Literals" [JLS 2013],
An integer literal is of type long if it is suffixed with an ASCII letter L or l (ell); otherwise, it is of type int. The suffix L is preferred because the letter l (ell) is often hard to distinguish from the digit 1 (one).
Consequently, use L
, not l
, to clarify programmer intent when indicating that an integer literal is of type long
.
Code Block | ||
---|---|---|
| ||
public class Visual { public static void main(String[] args) { System.out.println(11111 + 1111l); } } |
Compliant Solution
This compliant solution uses an uppercase L
(long
) instead of lowercase l
to disambiguate the visual appearance of the second integer. Its behavior is the same as that of the noncompliant code example, but the programmer's intent is clear:
...
This noncompliant example mixes decimal values and octal values while storing them in an array:. Integer literals with leading zeros denote octal values–not decimal values. According to §3.10.1, "Integer Literals" of the JLS [JLS 2013],
An octal numeral consists of an ASCII digit
0
followed by one or more of the ASCII digits0
through7
interspersed with underscores, and can represent a positive, zero, or negative integer.
This misinterpretation may result in programming errors and is more likely to occur while declaring multiple constants and trying to enhance the formatting with zero padding.
Code Block | ||
---|---|---|
| ||
int[] array = new int[3]; void exampleFunction() { array[0] = 2719; array[1] = 4435; array[2] = 0042; // ... } |
It appears that the The third element in array
is was likely intended to hold the decimal value 42. However, the decimal value 34 (corresponding to the octal value 42) gets is assigned.
Compliant Solution
...