equals Method in Java

In Java equals() is inherited from the Object class. By default equals method checks for reference equality, whether two objects are refreshed to the same body location. This is equivalent to using “==” for comparing objects.

If the Java class does not contain the equals method then the Object class equals method will be executed.

public class EqualsMethod {
    private int id;

    public EqualsMethod(int id) {
        this.id = id;
    }
    public static void main(String[] args) {
        EqualsMethod object1 = new EqualsMethod(10);
        EqualsMethod object2 = new EqualsMethod(20);
        System.out.println(object1.equals(object2));
    }
}
equals Method in Java

In the above Java code, the Object class equals method was executed which checks for reference equality (address comparison) that is if two references are pointing to the same object then only the equals method returns true.

equals() Overriding

Based on our requirement we can override equals method.

While overriding equals method we have to take care about the following.

  • What is the meaning of equality for example whether we have to check only name or only roll no or both for a student.
  • If we are passing different types of object then overrided equals method should not raise ClassCastException that is we have to handel ClassCastException to return false.
  • If we are passing Null argument then overrided equals method should not raise NullPointerException that is we have to handel NullPointerException to return false.
public class EqualsMethod {
    private int id;
    private String name;

    public EqualsMethod(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
    public boolean equals(Object obj) {
        try {
            String tempName = this.name;
            int tempId = this.id;
            EqualsMethod object = (EqualsMethod) obj;
            String tempName2 = object.name;
            int tempId2 = this.id;
            if (tempName.equals(tempName2) && tempId == tempId2) {
                return true;
            } else {
                return false;
            }
        }
        catch (java.lang.ClassCastException e){
            return false;
    }
        catch (java.lang.NullPointerException e){
            return false;
    }
    }
    public static void main(String[] args) {
        EqualsMethod object1 = new EqualsMethod(10, "TheCodeData");
        EqualsMethod object2 = new EqualsMethod(20, "Java");
        System.out.println(object1.equals(object2));
        }
    }
equals() Overriding

Note- To make above overrided equal method more efficent we have to write following code as the begining inside equals method.

 if (obj == this){
            return true;
        }

According to this if both references pointing to the same object then without perfroming any comparision equlas method returns true directly.

Similar Java Tutorials

2 thoughts on “equals Method in Java”

Leave a Comment