String Equality Use Equals
ID |
java.string_equality_use_equals |
Severity |
high |
Remediation Complexity |
trivial |
Remediation Risk |
low |
Remediation Effort |
low |
Resource |
Reliability |
Language |
Java |
Tags |
CWE:597, reliability, suspicious-comparison |
Description
Reports uses of == or != to compare String objects. These operators test reference equality, not content equality, leading to subtle bugs.
Rationale
The == operator checks whether two references point to the same object in memory, not whether they contain the same characters. While string literals may be interned, strings created at runtime (from new String(), substring(), concatenation, I/O, etc.) are typically distinct objects:
String a = getInput();
String b = getInput();
if (a == b) { // Bug: compares references, not content
process();
}