PB.DCP
Do not confuse the "+" operator for String concatenation
Description
This rule flags code where you might be confusing the "+" operator for `String' concatenation.
Example
The code below shows the trap you might fall into when using "+" the wrong way. The output of the following example is "2+9 = 29" not "2+9 = 11" as you might have expected.
package PB;
public class DCP {
public static void main (String args []) {
System.err.println ("2 + 9 = " + 2 + 9);
}
}
Repair
Calculate the result outside the System.out.println statement and print it, or enclose "2+9" in parentheses, i.e: "2+9 = " +(2+9)
|