I wonder why would anybody want to use identity comparison for fields in equals
, like here (Java syntax):
class C {
private A a;
public boolean equals(Object other) {
// standard boring prelude
if (other==this) return true;
if (other==null) return false;
if (other.getClass() != this.getClass()) return false;
C c = (C) other;
// the relevant part
if (c.a != this.a) return false;
// more tests... and then
return true;
}
// getter, setters, hashCode, ...
}
Using ==
is a bit faster than equals
and a bit shorter (due to no need for null tests), too, but in what cases (if any) you’d say it’s really better to use ==
for fields inside equals
?
8
There are cases when checking for instance equality with ==
is necessary. For example, that is what you do in the implementation of equals
to check if you are comparing an object to itself (a very important shortcut that also satisfies the reflexivity requirement of equals
)
if (other==this) return true;
The other case is when the system guarantees that there will be only one instance that is equal to any object of a particular class (and by necessity that is going to be the instance itself – again, due to reflexivity of equals
). This is another common case illustrated by your code fragment:
if (other.getClass() != this.getClass())
An equality of two java.lang.Class
objects is equivalent to the equality of their references, because for each class that you define there is one, and only one, instance of java.lang.Class
.
A third case where reference equality is desired is serialization: you need to keep track of object instances by tracking their references, even if they are logically equal, in order to be able to produce an isomorphic graph upon deserialization.
EDIT : Inside implementations of equals
the use of ==
boils down to increasing performance: comparing an object to itself is a common case, especially when you use objects of the class as keys in a hash table, so it makes sense to optimize it.
3
The == operator
in Java checks for reference equality: it returns true if the pointers are the same. It does not check for contents equality.
Use the equals() method to compare object values.
The equals() method returns a boolean value.
The two operators that can be used with object references are comparing for equality (==)
and inequality (!=).
These operators compare two values to see if they refer to the same object. Although this comparison is very fast, it is often not what you want.
Usually you want to know if the objects have the same value, and not whether two objects are a reference to the same object. For example,
if (name == "My Test Value") // Legal, but ALMOST SURELY WRONG
This is true only if name is a reference to the same object that “My Test Value” refers to. This will be false if the String in name was read from input or computed (by putting strings together or taking the substring), even though name really does have exactly those characters in it.
Many classes (eg, String) define the equals()
method to compare the values of objects.
3
It is true that very often we use ==
instead of equals()
only for performance, where calling equals()
would do just as well, but there exist cases where using ==
is necessary.
For example, if you have a list which allows duplicate values, (multiple different instances that appear equal according to their equals()
methods,) and you want to remove a particular instance from that list, you cannot use equals()
to find the instance to remove, because equals()
would find you the first duplicate, not the exact instance that you are looking for. To find the exact instance, you must use ==
.
Of course, this is just a simplistic example. In order to find out more about why both ==
and equals()
are absolutely necessary, you need to read up on the concept of value semantics vs. reference semantics. (I searched for some good source of information on the subject but could not find anything well written; if someone finds something good, please post!)
It’s indeed rare when you want to do reference (==
) equality instead of logical (equals()
) equality. Often when you find yourself checking for reference equality, there might be something wrong with your design, or you might want to implement the equals()
method.
Nevertheless, there is one case where I often check for reference equality (not sure if that’s good practice):
public void actionPerformed(ActionEvent e){
AbstractButton source = (AbstractButton) e.getSource();
if (source == someButton)
doStuff();
else if (source == someOtherButton)
doSomethingElse();
// ...
}