equals method

equals() in Object Class


  • According to Object class, equals( ) act unlike equals operator i.e.( ==). It will compare two similar objects and return output as Boolean value.

  • If both objects are pointing to the same memory location like p1==p2 then the output is true else false i.e. p1==p3.


  • Syntax of equals method :


    public boolean equals(Object obj)
    {
      return (this.hashCode( ) == obj.hashCode( ));
    }

    Before overriding equals( ):

    • Before overriding ,equals( ) will act unlike equals operator as described in below program.
    • Example 1:

       
                      
                   package com.sdj;
                   class Person
                   {
                      String name;
                      int age;
                      public Person(String name,int age) 
                      {
                         this.age=age;
                         this.name=name;
                      }
                      public String toString()
                      {
                         return"The Name of the Person:"+this.name+","+"The age of the person is:"+this.age;
                      }
                   }                
       
                      
                   public class OverrideToStringDemo
                   {
                      public static void main(String[] args)
                      {
                         Person p1=new Person("Praveen",24);
                         System.out.println(p1);
                         Person p2=new Person("Praveen",24);
                         Person p3=new Person("Sujata",23);
                         Person p4=new Person("Sujata",23);
                         System.out.println(p1.equals(p2));
                         System.out.println(p3.equals(p4));
                      }
                   }                

         Output:

      The Name of the Person:Praveen,The age of the person is:24 false false

    After overriding equals( ):

    • After overriding equals( ), the above program become like the below.
    • After overriding equals( ) , it compares two objects based on the content of the object but not based on the memory address.
    • Example 2:

       
                      
                   package com.sdj;
                   class Person
                   {
                      String name;
                      int age;
                      public Person(String name,int age) 
                      {
                         this.age=age;
                         this.name=name;
                      }
                      /*   overriding equals method is based on the content of object as below. */
                      /*   Here content specifies state of the object. */
                      public boolean equals(Object obj) 
                      {
                         Person p=(Person) obj;.
                         if (this.name==p.name&&this.age==p.age)
                         {
                            return true;
                         }
                         else
                         {
                            return false;
                         }
                      }
                      public String toString()
                      {
                         return "The Name of the Person:"+this.name+","+"The age of the person is:"+this.age;
                      }
                   }
                   public class OverrideToStringDemo 
                   {
                      public static void main(String[] args) 
                      {
                         Person p1=new Person("Praveen",24);
                         System.out.println(p1);
                         Person p2=new Person("Praveen",24);
                         Person p3=new Person("Sujata",23);
                         Person p4=new Person("Sujata",23);
                         System.out.println(p1.equals(p2));
                         System.out.println(p3.equals(p4));
                      }
                   }                

         Output:

      The Name of the Person:Praveen,The age of the person is:24 true true
      Note
      • Before overriding p1.equals(p2) and p3.equals(p4) both will return false After overriding p1.equals(p2) and p3.equals(p4) both will return true.

      • Incase of overriding ,equals( ) method works based on the content of the Object but not based on the memory location of the Object.