It’s almost always a mistake to compare two instances of java.lang.String
or boxed types like java.lang.Integer
using
reference equality ==
or !=
, because it is not comparing actual value but locations in memory.
String firstName = getFirstName(); // String overrides equals String lastName = getLastName(); if (firstName == lastName) { ... }; // Non-compliant; false even if the strings have the same value
String firstName = getFirstName(); String lastName = getLastName(); if (firstName != null && firstName.equals(lastName)) { ... };