CRITICAL
Rule Definition
It is a source of great confusion to novice programmers that Java has two ways of thinking about the equality of objects. When used with object references, the == operator returns true only if both references are to the same object. The == operator is used to compare values of primitive types or object references, but it does not test for value or semantic equality. This is the same for the '!=' operator.
Remediation
In the case of a semantic equality replace it with a call to equals method.
Violation Code Sample
int i = 1;
int j = 2;
Integer ia;
Integer ib;
if (i == j) {} // OK: i and j are primitive types
if (ia == ib) {} // VIOLATION
if (ia != ib) {} // VIOLATION
Fixed Code Sample
Integer ia;
Integer ib;
if (ia.equals(ib)) {} // FIXED
if (!ia.equals(ib)) {} // FIXED
Reference
Practical Java Programming Language Guide - ISBN 0-201-61646-7, http://javatechniques.com/blog/string-equality-and-interning
Related Technologies
JEE
Technical Criterion
CWE-597 - Use of Wrong Operator in String Comparison
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.