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?
virtual operator==gets used in place of java'sequalsand pointer comparison gets used in place of java's==, so I see there more or less 1:1 correspondence. But feel free to add the tag if you're confident it belongs there. – maaartinus Oct 5 '12 at 23:54