a == b; what does it means?
a and b are reference variables of the same type and if we test equality between these two variables we are testing whether they have the same reference.
Some programing languages gives us implementations for standard types like integers and strings. In Java we can test the equality of two Strings, a and b, with a.equals(b).
When we want to test equality on our own types like Person, we need to override equals(). In order to correctly override the equals() method we must follow a few rules; the relation between the objects must be:
In order to follow these natural definitions we need to implement them:
public boolean equals(Object b) {
if (this == b) return true;
if (b == null) return false;
if (this.getClass() != b.getClass()) return false;
Person other = (Person) b;
if (this.firstName != other.firstName) return false;
if (this.lastName != other.lastName) return false;
return true;
}
This implementation can be used for any custom type.