Inspectopedia
 
2024.3

'equals()' called on classes which don't override it

Warning
New
Last modified: 03 December 2024

Reports equals() calls on StringBuilder, StringBuffer and instances of java.util.concurrent.atomic package.

The equals() method is not overridden in these classes, so it may return false even when the contents of the two objects are the same. If the reference equality is intended, it's better to use == to avoid confusion.

A quick-fix for StringBuilder, StringBuffer, AtomicBoolean, AtomicInteger, AtomicBoolean and AtomicLong is available to transform into a comparison of contents. The quick-fix may change the semantics when one of the instances is null.

Example:

After the quick-fix is applied:

New in 2017.2