tags:

views:

412

answers:

6

What value is hashCode() method is returning in java?. i read that it is an memory reference of an object. when i print hascode value for new Integer(1), its 1. for String(a) - 97. so i confused. is it ascii or what type of value is?

+6  A: 

A hashcode is an integer value that represents the state of the object upon which it was called. That is why an Integer that is set to 1 will return a hashcode of "1" because an Integer's hashcode and its value are the same thing. A character's hashcode is equal to it's ASCII character code. If you write a custom type you are responsible for creating a good hashCode implementation that will best represent the state of the current instance.

Andrew Hare
+6  A: 

The value returned by hashCode() is by no means guaranteed to be the memory address of the object. I'm not sure of the implementation in the Object class, but keep in mind most classes will override hashCode() such that two instances that are semantically equivalent (but are not the same instance) will hash to the same value. This is especially important if the classes may be used within another data structure, such as Set, that relies on hashCode being consistent with equals.

If you want a hashCode() that uniquely identifies an instance no matter what, use System.identityHashCode() - this will delegate to the default hashCode method regardless of whether it has been overridden.

danben
Any particular reason this was modded down? If something here is incorrect I would love a correction, but to downvote without explanation adds nothing to the discussion.
danben
A few years ago, Ted Neward explained in http://blogs.tedneward.com/2008/07/16/ObjecthashCode+Implementation.aspx how the OpenJDK implemented Object.hashCode(). The OpenJDK derives the hash code from the object address, but caches this value and returns it to subsequent callers in case the object moves in memory and its address changes.After briefly reviewing the latest code, I found that the implementation seems not to have changed since Neward wrote his article.
Derek Mahar
This would seem to support my answer.
danben
+2  A: 

The hashCode() method is often used for identifying an object. I think the Object implementation returns the pointer (not a real pointer but a unique id or something like that) of the object. But most classes override the method. Like the String class. Two String objects have not the same pointer but they are equal:

new String("a").hashCode() == new String("a").hashCode()

I think the most common use for hashCode() is in Hashtable, HashSet, etc..

Java API Object hashCode()

polyurethan
+1  A: 

Object.hashCode(), if memory serves correctly (check the JavaDoc for java.lang.Object), is implementation-dependent, and will change depending on the object (the Sun JVM derives the value from the value of the reference to the object).

Note that if you are implementing any nontrivial object, and want to correctly store them in a HashMap or HashSet, you MUST override hashCode() and equals(). hashCode() can do whatever you like (it's entirely legal, but suboptimal to have it return 1.), but it's vital that if your equals() method returns true, then the value returned by hashCode() for both objects are equal.

Confusion and lack of understanding of hashCode() and equals() is a big source of bugs. Make sure that you thoroughly familiarize yourself with the JavaDocs for Object.hashCode() and Object.equals(), and I guarantee that the time spent will pay for itself.

Ben Fowler
+1  A: 

Object.hashCode() used to return a memory address about 14 years ago. Not since.

EJP
+3  A: 

If you want to know how they are implmented, I suggest you read the source. If you are using an IDE you can just + on a method you are interested in and see how a method is implemented. If you cannot do that, you can google for the source.

For example, Integer.hashCode() is implemented as

   public int hashCode() {
       return value;
   }

and String.hashCode()

   public int hashCode() {
       int h = hash;
       if (h == 0) {
           int off = offset;
           char val[] = value;
           int len = count;

           for (int i = 0; i < len; i++) {
               h = 31*h + val[off++];
           }
           hash = h;
       }
       return h;
   }
Peter Lawrey
I've planned to answer precisely the same way; +1
incarnate