...
Using reference equality in place of object equality is permitted only when the defining classes guarantee the existence of at most one object instance for each possible object value. This generally requires that instances of such classes are immutable. The use of static factory methods, rather than public constructors, facilitates instance control, which is a key enabling technique.
Objects that are instances of classes that provide this guarantee obey the invariant that for any two references a
and b
, a.equals(b)
is exactly equivalent to a == b
[Bloch 2008]. The String
class fails to meet these requirements and consequently fails to preserve this invariant.
Use reference equality to determine whether two references point to the same object.
...