CMP.STR

This warning appears if string references are compared rather than strings themselves for String type.

Vulnerability and risk

This problem can cause unexpected application behavior. Comparing objects using == usually produces deceptive results, since the == operator compares object references rather than values. To use == on a string, the programmer has to make sure that these are constant strings, statically created in the same class or "interned" prior to comparison using the intern() method.

Mitigation and prevention

Use the equals() method to compare objects instead of the == operator.

Example 1

Copy
     /**
      *  Return symbolic name of operation
      */
     public String nameOperation(String key) {
         if (key == "++") return "PLUS";
         if (key == "--") return "MINUS";
         return "UNKNOWN";
     }

CMP.STR is reported for line 14: Comparing strings 'key' and '++' with ==CMP.STR is reported for line 15: Comparing strings 'key' and '--' with ==