hashCode method

hashCode() in Object Class


  • hashCode( ) will play an important role inside the Object class.

  • It will return the memory location of the current object in integer format.


  • Syntax of hashCode method :


    public int hashCode( );

  • hashCode ( ) will always try to prove two objects to be equal.

  • Example 1:

     
                    
                 package com.sdj;
                 class Person
                 {
                    String name;
                    int age;
                    public Person(String name,int age) 
                    {
                       this.age=age;
                       this.name=name;
                    }
                    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(p1.hashCode()==p2.hashCode());
                       System.out.println(p3.equals(p4));
                       System.out.println(p3.hashCode()==p4.hashCode());
                    }
                 }                

       Output:

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

    Explanation:

    • In the above program p1.equals(p2) and p3.equals(p4) will return true ,but p1.hashCode() = = p2.hashCode() and p3.hashCode() = = p4.hashCode() will return false.

    • Means that equals( ) is telling p1 and p2 objects are same ,But hashCode() is telling p1 and p2 objects are different . But it is not meaningfull because one telling 2 objects are equal but another one telling same 2 objects not equal.

    • In order to make this meaningful we need to override both equals() and hashCode( ) inside our class based on content/state of object. Either one get overrids its not advisble.

    javapadho
    Memory manage by hasCode()

    Example 2:

     
                    
                 package com.sdj;
                 class Person
                 {
                    String name;
                    int age;
                    public Person(String name,int age) 
                    {
                       this.age=age;
                       this.name=name;
                    }
                    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(p1.hashCode()==p2.hashCode());
                       Person p5=p2;
                       System.out.println(p5.equals(p2));
                       System.out.println(p5.hashCode()==p2.hashCode());
                       System.out.println(p3.equals(p4));
                       System.out.println(p3.hashCode()==p4.hashCode());
                    }
                 }                

       Output:

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

    Explanation:

    • In the above program p1.equals(p2) returns true value and p1.hashCode( )==p2.hashCode( ) will return false value.

    • One method trying to say both are equal and the another method trying to say both objects are different.

    • Because here equals( ) has overrided based on the content of the object and hashCode() does not overrided based on content it’s works based on memory location of the object.

    • So in order to prove two objects are equal then we should override the both equals( ) and hashCode( ) based on the content only.

    How to override hashCode( ):

      

    Steps to override hashCode( ):

    • We have to convert all Object values into String Type.

    • Then Convert it into hashcode by calling the corresponding hashcode( ) method

    • Add all hashCodes and Return it.

    After overriding hashCode( ):

     
                    
                 package com.sdj;
                 class Person
                 {
                    String name;
                    int age;
                    public Person(String name,int age) 
                    {
                       this.age=age;
                       this.name=name;
                    }
                    public boolean equals(Object obj) 
                    {
                       Person p=(Person) obj;
                       if (this.name==p.name&&this.age==p.age)
                       {
                          return true;
                       }
                       else
                       {
                          return false;
                       }
                    }
                    public int hashCode( ) 
                    {
                       /*Converting age attribute in to String Type*/
                       String strAge=Integer.toString(this.age);
                       int hashCode=this.name.hashCode()+strAge.hashCode();
                       return hashCode;
                    }
                    public String toString( ) 
                    {
                       return"The Name of the Person:"+this.name+","+"The age of the person is:"+this.age;
                    }
                 }
                 public class OverrideToStringDemo 
                 {
                    public staticvoid 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(p1.hashCode()==p2.hashCode());
                       Person p5=p2;
                       System.out.println(p5.equals(p2));
                       System.out.println(p5.hashCode()==p2.hashCode());
                       System.out.println(p3.equals(p4));
                       System.out.println(p3.hashCode()==p4.hashCode());
                    }
                 }                

       Output:

    The Name of the Person:Ravi.p, The age of the person is:24 true true true true true true

    Explanation:

    javapadho
    Memory manage by hasCode()

    toString( ),equals( ) and hashCode( ) in String class:

    • equals( ) and hashCode( ) has already overrided in String class based on the content of the object but not based on the memory address.

    The following program describes very clearly:

     
                    
                 package com.sdj;
                 public class StringOverrideDemo 
                 {
                    public static void main(String[ ] args) 
                    {
                       String s1=new String("Praveen");
                       String s2=new String("Praveen");
                       System.out.println(s1.equals(s2));
                       System.out.println(s1.hashCode()==s2.());
                       System.out.println(s1);
                    }
                 }                

       Output:

    Output: true true Praveen

    Explanation:

    • In the above program s1.equals(s2) and s1.hashCode( )==s2.hashCode( ) will return true value even though we does not override equals( ) and hashCode( ) inside our class.

    • Means inside String class equals( ) and hashCode( ) has already overrided based on the content.

    • Along with the equals( ) and hashCode( ), toString( ) also overrided in String class.

    • Even though we pass derived object as input parameter to System.Out.println(derived Object) we don’t get the memory address in hexadecimal format because toString( ) has overrided in String classes.