Equals on Unrelated Types

ID

java.equals_unrelated_types

Severity

low

Remediation Complexity

trivial

Remediation Risk

low

Remediation Effort

low

Resource

Reliability

Language

Java

Tags

CWE:597, reliability

Description

Reports calls to a.equals(b) where a and b are provably different, unrelated types. Such a comparison always returns false and usually indicates a programming mistake.

Rationale

Comparing a String with an Integer, or any two unrelated wrapper types, can never be true. This usually indicates that the wrong variable was passed or that a type conversion is missing.

String name = "Alice";
name.equals(Integer.valueOf(42)); // Always false

Remediation

Ensure both sides of the equals comparison are the same type or convert them appropriately.