Java defines the equality operators ==
and !=
for testing reference equality but uses the Object.equals()
method and its subclasses for testing abstract object equality. Naive programmers often confuse the intent of the ==
operation with that of the Object.equals()
method. This confusion is frequently evident in the context of processing String
processing objects.
As a general rule, use the Object.equals()
method to check whether two objects are abstractly equal to each other and the equality operators ==
and !=
to test whether two references specifically refer to the same object. This latter test is referred to as referential equality. Also see MET09-J. Classes that define an equals() method must also define a hashCode() method .
...