Inspectopedia
 
2024.3

'equals()' called on itself

Warning
New
Last modified: 03 December 2024

Reports calls to equals(), compareTo() or similar, that compare an object for equality with itself. The method contracts of these methods specify that such calls will always return true for equals() or 0 for compareTo(). The inspection also checks calls to Objects.equals(), Objects.deepEquals(), Arrays.equals(), Comparator.compare(), assertEquals() methods of test frameworks (JUnit, TestNG, AssertJ), Integer.compare(), Integer.compareUnsigned() and similar methods.

Note that in rare cases, the inspection may report equals() calls that return false, because while the expressions on the both sides are the same, they produce separate objects, and comparison is performed on references, rather than on content. The simplest example is new Object().equals(new Object()). In any case, such calls are suspicious, and likely something else was intended.

Example: