Thursday, May 1, 2014

HashCode and Equals--> All time favourite

We all know that there are two methods hashCode() and equals() present in Object class. We have always heard of that hashCode is used to give the hashcode of an object which is nothing but the memory address of the object stored and equals is used for equivalence relation match or to test whether two objects are equal or not.
Well this sounds good, but a lack of proper knowledge in these aspects will lead you to in trouble and when the interviewer asks about these concepts in detail, you will definitely get confused. So to make it more easy to understand and to practically implement these methods in our application, i have come up with this post.

HashCode method is defined in java.lang package which is by default inherited in any class. The hashCode method returns an integer value which is nothing but it is derived by mapping the memory address to an integer value. It is a native method defined in Object class of java.lang package that is it is implemented directly in the native code in the JVM.





public native int hashCode();

We can override this method in our own class to give our own implementation. We will discuss it with an example. Before that i wanted to tell you that All wrapper classes in java including Integer,Float,Character implements hashCode() method.


The String class also implements its own hashCode method inherited from the object class and overrides It.

Now lets take an example of Employee Class.We have employeeId, employeeName,employeeAge as an attribute and when i am implementing hashCode() in this class i am returning employeeAge in terms of its hashCode().

package com.javalearninghub;
/*** @author Ankur Jain
*
*/
public class Employee 
{
private int employeeAge ;
public Employee( int employeeAge)
{
super();
this.employeeAge = employeeAge;
}
public int hashCode(){
return employeeAge;
}
}


In this class we are returning employeeAge as a hashCode(). Now test class for the same will be:-

package com.javalearninghub;
import com.javalearninghub.Employee;
/**
@Author Ankur Jain
**/
public class TestMain{
public static void main(String args[]){
Employee e1= new Employee(23);
System.out.println("The hashCode for the above is:"+e1.hashCode());
}
}

The above code will return 23 as the hashCode of an employee object e1. Now what if i have to get the original hashCode of the the employee object e1 even after overriding hashCode() method. Java provides us with a system API from where you can get the actual hashCode(). which is
System.identityHashCode(object);
which takes an object as a parameter of any class.

So,to add up with the above we can have the above code modified as:-

package com.javalearninghub;
import com.javalearninghub.Employee;
/**
@Author Ankur Jain
**/
public class TestMain{
public static void main(String args[]){
Employee e1= new Employee(23);
System.out.println("The hashCode for the above is:"+e1.hashCode());
int original=System.identityHashCode(e1);
System.out.println("The original hashCode is:"+original);
}
}

We will discuss about equals method in our next article.


No comments:

Post a Comment