What is HashCode in Java?

Here you will learn about hashcode in Java.

Hash code is an integer returned by hashCode() method. It has many advantages in different applications in java. Let us see how this method is giving hash code, how it is useful and what is the significance in java?

What is HashCode in Java?

Based on hash code we can keep objects in hash buckets. Some algorithms or data structures will use these hash buckets. Mostly hash based data structures like hashmap, hashset, hashtable will use this hash code. hashCode() method is provided by every class is either explicitly or implicitly. It is specified in java.lang.Object class. This method will return an integer as a result by mapping an integer to internal memory address in which object is stored. This hash code is same whenever we called hashCode() method with same object more than once in same execution. But in different execution of same application may give different integer.

Different objects can have same hash code because hash code is 32 bit signed integer. Total number of different possible hash values are 2^32. But total number of different objects can be more than 2^32. So hash code can be repeated. Then collision will happen. In this case we need to select the particular object that we need from that specific hash bucket. Hash buckets will reduce the search space for searching. Too many collisions will decrease the performance.

We can override this function when it is needed. But equals() method must be overridden  whenever hashCode() method has been over ridden. This is because whenever two objects are equal according to equals() method then they should return the same hash code. But we can’t say if hash code is same then objects are same. See the below picture for clear understanding.

What is HashCode in Java

Image Source

class HashExample{
	int a;
	public HashExample(int number){
		a=number;
	}
	
	public static void main(String args[])
	{
		HashExample obj1= new HashExample(5); //creating two different objects
		HashExample obj2= new HashExample(6);
		
		System.out.println("obj1 hash code when first time calling " + obj1.hashCode());
		System.out.println("obj2 hash code when first time calling " + obj2.hashCode());
		System.out.println("obj1 hash code when second time calling " + obj1.hashCode());
		System.out.println("obj2 hash code when second time calling " + obj2.hashCode());
	}
}

Output

obj1 hash code when first time calling 356573597
obj2 hash code when first time calling 1735600054
obj1 hash code when second time calling 356573597
obj2 hash code when second time calling 1735600054

The hashcode() method behavior can be seen in above specified output. Same object is having same hash code in current execution of the program provided that no information available about equals() method comparison fields.

Overriding

We can override the hashCode() method. Several reasons will be there to override this function. Good overriding will give best performance by reducing collisions. By overriding, different hash code can be provided for different objects hence performance can be improved. After overriding also we can get original hash code which is provided by jvm to object by using identityHashCode() method.

Example program for overriding:

class HashExample{
	int a;
	
	public HashExample(int number){
		a = number;
	}
	
	public int hashCode()    // overriding hashCode() method
	{
		return a*17;
	}
	
	public static void main(String args[])
	{
		HashExample obj1 = new HashExample(5); //creating two different objects
		HashExample obj2 = new HashExample(6);
		
		System.out.println("obj1 hash code when first time calling " + obj1.hashCode());
		System.out.println("obj2 hash code when first time calling " + obj2.hashCode());
		System.out.println("obj1 hash code when second time calling " + obj1.hashCode());
		System.out.println("obj2 hash code when second time calling " + obj2.hashCode());
		
		// to get original hash code for obj1 
		System.out.println("obj1 original hash code " + System.identityHashCode(obj1));
	}
}

Output

obj1 hash code when first time calling 85
obj2 hash code when first time calling 102
obj1 hash code when second time calling 85
obj2 hash code when second time calling 102
obj1 original hash code 356573597

It is very easy to understand but we should be careful with usage of hash code.

  • We should not use hash code as a key value because it can be repeated.
  • We should not use hash code in distributed scenarios because hashCode() is natively implemented. It will behave different in one environment to other environment. Hence for recognizing object we can’t rely on hash code.
  • We should not interpret hash code is memory address. Both are different.

Comment below if you have any queries regarding above tutorial for hashcode in java.

Leave a Comment

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