Well, hashcode is very common term in Java programming. Since Java makes use of hash based data structures, it provides inbuilt support through hashcode. The hashcode method is inherited by all classes from the Object class. This method returns a 32 bit integer which is unique for objects in a 32 bit machine.

Hashcode and ==

For 32 bit machines, you will always get true as the result of invoking hashcode method for the same object. For different objects it will always be false.

For 64 bit machines, the use of == operator with hashcode becomes tricky because the method returns a 32 bit int value which can not represent all memory addresses for 64 bit architecture. Thus in this case, two different objects can have same hashcode. However for 64 bit machine, using == with hashcode of same object will always return true.

The following code and the output show that the hashcode of an object will always be same:

package com.example;

public class Test{

	public static void main(String[] args) {
		Test t = new Test();
		System.out.println(t.hashCode());
		System.out.println(t.hashCode());
		Test t1 = new Test();
		System.out.println(t1.hashCode());
		System.out.println(t1.hashCode());
	}
}

Output:

259937031
259937031
779942019
779942019

Hashcode Computation Algorithm

The sourcecode of Object class shows the hashcode method as a native method which means that the code for this method is written in a low level programming language like C. The javadoc for this method in the Object class quotes the following about its implementation:

As much as is reasonably practical, the hashCode method defined by
class {@code Object} does return distinct integers for distinct
objects. (This is typically implemented by converting the internal
address of the object into an integer, but this implementation
technique is not required by the
JavaTM programming language.)

Overriding Hashcode

There are few rules which any Java programmer is taught to follow (at least in theory). The equals and hashcode should always be followed. But apart from that an efficient hashcode method is required for the objects of class to be practically used with hashing based data structures.

In fact, there are some best practices released and used by String class for overriding the hashcode method. But the task of overriding the hashcode and providing a good implementation can easily be handled by third party libraries like Apache Commons HashBuilder.

Dive deep into Hashcode Internals admin Core Java
Well, hashcode is very common term in Java programming. Since Java makes use of hash based data structures, it provides inbuilt support through hashcode. The hashcode method is inherited by all classes from the Object class. This method returns a 32 bit integer which is unique for objects in...
<p>Well, hashcode is very common term in Java programming. Since Java makes use of hash based data structures, it provides inbuilt support through hashcode. <strong>The hashcode method is inherited by all classes from the Object class.</strong> This method returns a 32 bit integer which is unique for objects in a 32 bit machine.</p> <h2>Hashcode and ==</h2> <p><strong>For 32 bit machines</strong>, you will always get true as the result of invoking hashcode method for the same object. For different objects it will always be false. <strong>For 64 bit machines</strong>, the use of == operator with hashcode becomes tricky because the method returns a 32 bit int value which can not represent all memory addresses for 64 bit architecture. <strong>Thus in this case, two different objects can have same hashcode</strong>. However for 64 bit machine, using == with hashcode of same object will always return true. The following code and the output show that the hashcode of an object will always be same: </p> 1 <blockquote>Output: 259937031 259937031 779942019 779942019</blockquote> <h2>Hashcode Computation Algorithm</h2> <p>The sourcecode of Object class shows the hashcode method as a native method which means that the code for this method is written in a low level programming language like C. The javadoc for this method in the Object class quotes the following about its implementation:</p> <blockquote> As much as is reasonably practical, the hashCode method defined by class {@code Object} does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java<font size="-2"><sup>TM</sup></font> programming language.)</blockquote> <h2>Overriding Hashcode</h2> <p>There are few rules which any Java programmer is taught to follow (at least in theory). The equals and hashcode should always be followed. But apart from that <strong>an efficient hashcode method is required</strong> for the objects of class to be practically used with hashing based data structures. In fact, there are some best practices released and used by <a href="http://en.wikipedia.org/wiki/Java_hashCode%28%29#The_java.lang.String_hash_function" target="_blank">String class</a> for overriding the hashcode method. But the task of overriding the hashcode and providing a good implementation can easily be handled by third party libraries like <a href="http://commons.apache.org/lang/api-2.6/org/apache/commons/lang/builder/HashCodeBuilder.html" target="_blank">Apache Commons HashBuilder</a>.</p>
The following two tabs change content below.
I run this blog with lots of passion. In this website, you will find tutorials on Core Java, Spring, Struts, Web Applications, Portals and Database. Please support me and the website by sharing the posts on your facebook / twitter. You can tap the share button at the top of each post. Thanks for the support.