Sponsored Links

Ad by Google
In java parent of all classes is Object, Object class is a super class of every classes and available inside java.lang package. hashCode() method is one of the Object class method mean every object implicitly or explicitly has a hashCode() method which returns a 32 bit signed integer number.
The integer number return by the hashCode() method is the internal memory address of the object and it's useful when using the objects in a hash based collections or other algorithms that rely on hash codes such as HashMap, HashSet and Hashtable.
hashCode() method is a native method so the implementation of hashCode() method is available inside the native code in JVM.
general contract of hashCode() method are:
  • whenever you invoked hashCode() method on the same object more than once during the execution of an application, the hashCode() method must return the same integer number.
For example if you run the below statements more than once it'll return you the same integer number.
public static void main(String[] args) {
  String fruit = "apple";
  System.out.println("hash value of apple is=> "+fruit.hashCode());
Out put:
hash value of apple is=> 93029210
  • If two objects are equal according to the equals(Object) method,then the calling hashCode() method on each of the objects must return the same integer number.
For example try to run the below statement and see the output of both the objects are same.
public static void main(String[] args) {
  String first = "abcd";
  String second = "abcd";
  System.out.println("equals method return => "+first.equals(second));
  System.out.println("hashCode must be same..");
  System.out.println("first hashCode is=> "+first.hashCode());
  System.out.println("second hashCode is=> "+second.hashCode());
Out put:
equals method return => true
hashCode must be same..
first hashCode is=> 2987074
second hashCode is=> 2987074
  • It is not necessarily that, if two objects are unequal according to the equals(object) method,then calling hashCode() method on each of the objects must return the unique integer number.
For example here both string is not equal as per equals() method but hashCode is same
public static void main(String[] args) {
String first = "Aa";
  String second = "BB";
  System.out.println("both string equality is "+first.equals(second));
  System.out.println("first hasCode is "+first.hashCode());
  System.out.println("second hashCode=> "+second.hashCode());
  System.out.println("both hashCode are same..");
Out put:
both string equality=> false
first hasCode=> 2112
second hashCode=> 2112
both hashCode are same..

Whenever two different objects have the same hash code, we call this a collision. Collision means that there is more than one object available in a single bucket, you can read more on this on my next post.
If you are planing to use your objects inside the hash based collection you must need to override equals(Object) and hashCode() method, and provide the better implementation of hashCode() method. Overriding of hashCode() and equals() method are explain separately here.

Don't forgot to post your comments/feedback/suggestion about this post or if you have something to add on this.
Sponsored Links


Post a Comment