String literals, just like any other Object
, should be compared using the equals()
method.
Using ==
and !=
does not work in general.
The following code:
if (variable == "foo") { /* ... */ } // Non-Compliant if (variable != "foo") { /* ... */ } // Non-Compliant
should be refactored into:
if ("foo".equals(variable)) { /* ... */ } // Compliant if (!"foo".equals(variable)) { /* ... */ } // Compliant