The equals method is a fundamental method of the Object class in Java. This method allows you to compare two objects to determine if they are equal.
To use the equals method correctly, it is important to follow some rules. First, the equals method must be appropriately overridden in subclasses. Additionally, the equals method must be reflexive, symmetric, transitive and consistent.
The equals method must be reflexive, symmetric, transitive and consistent. These properties are called equivalence properties and are important to ensure that the equals method works correctly.
- Reflexivity: for any non-null object
x,x.equals(x)must returntrue. - Symmetry: for any pair of non-null objects
xandy,x.equals(y)must returntrueif and only ify.equals(x)returnstrue. - Transitivity: for any triplet of non-null objects
x,y, andz, ifx.equals(y)returnstrueandy.equals(z)returnstrue, thenx.equals(z)must also returntrue. - Consistency: for any pair of non-null objects
xandy, multiple calls tox.equals(y)must consistently return the same value, unless the information used in equality comparisons is modified.
These algebraic properties help ensure that the equals method works predictably and consistently when used to compare objects in Java.
Here is an example of how to override the equals method in a subclass:
public class Human {
private String firstName;
private String lastName;
private String passportNumber;
public Human(String firstName, String lastName, String passportNumber) {
this.firstName = firstName;
this.lastName = lastName;
this.passportNumber = passportNumber;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Human human = (Human) o;
return Objects.equals(firstName, human.firstName) &&
Objects.equals(lastName, human.lastName) &&
Objects.equals(passportNumber, human.passportNumber);
}
}
In this example, we have overridden the equals method in the Human class to compare two Human objects based on their firstName, lastName, and passportNumber attributes.
Always remember to use the equals method correctly when working with objects in Java!
