Why do we have to override the equals() method in Java?

Why do we have to override the equals() method in Java?

Why do we have to override the equals() method in Java?

Why do we have to override the equals() method in Java?
  • We can use the equals method to check equality of two objects.
  • The equals() method of java.lang.Object class acts the same as the == operator; that is, it tests for object identity rather than object equality. The implicit contract of the equals() method, however, is that it tests for equality rather than identity. Thus most classes will override equals() with a version that does field by field comparisons before deciding whether to return true or false.
  • Java.lang.Object has methods called hasCode() and equals(). These methods play a significant role in the real-time application. However, its use is not always common to all applications. In some case, these methods are overridden to perform certain purpose.
  • If our class doesn’t contain equals() then object class equals() will be executed.
import java.util.*; 
class Student{ 
 String name; 
 int rollNo;
 Test( String s, int r) { 
  this.name=s; 
  this.rollNo=r;
 } 
 public static void main(String args[]) { 
  Student t1=new Student("Raja",101); 
  Student t2=new Student("Raja", 101); 
  Student t3=new Student("Rani",103); 
  Student t4=t1; 
  System.out.println(t1.equals(t2)); :- false 
  System.out.println(t1.equals(t3)); :- false 
  System.out.println(t1.equals(t4)); :- true 
 } 
}
  • In the above example object class equals() method got executed which is meant for reference comparison(address comparison)
  • That is if two references pointing to the same object then only equals() return true.
  • Without overriding the equals() method, it will act like “==”. When you use the “==” operator on objects, it simply checks to see if those pointers refer to the same object. Not if their members contain the same value.
  • Based on our requirement we can override equals() method for content comparison.
  • While overriding equals() for content comparison we have to take care of the following
    1. What is the meaning of equality(i.e. Whether we have to check the only name or only roll number or both)
    2. If we are passing the different type of object our equals method should not rise ClassCastException i.e we have to handle
      ClassCastException to return false.
    3. If we are passing null argument then our equals() should not rise NullPointerException i.e we have to handle
      NullPointerException to return false.
  • The following is the proper way to overriding equals() for student class content comparison.
  • public boolean equals(Object obj) {
      if (obj==this) {
       return true;
      }
       if (obj instanceof Test) {
          Test t=(Test)obj;
          if (name.equals(t.name) && rollNo==t.rollNo) {
       return true;
       }
         else
      return false;
          }
        else
         return false;
     }

Leave a Reply

Your email address will not be published. Required fields are marked *

*