3

Possible Duplicate:
what is an objects hashcode
How do hashCode() and identityHashCode() work at the back end?

I'm not talking about String class or any other class where the hashcode is overridden. Say if I just create a new object of the Object class, then will the hashcode() or to be true in any case, identityHashCode(Object x) return the memory address of that object?

6
  • I'm pretty sure that it doesn't on OpenJDK, or we would see much more patterns in the identityHashCode() of consecutively created objects. (Try it, create 100 objects, print their hash code and try to find a pattern). Commented Jun 6, 2012 at 16:01
  • Last I looked, the default implementation, if the specific class didn't override it, was to return an integer derived from the address of the object at the time the hash code was first requested. Of course, for a copying GC the address may change, so the implementation (if it uses the address) must be careful to cache the hash code first produced and always return that, vs re-deriving it. There is no requirement that the hash code have any relation to the address, and for some objects (eg, Strings) it will most definitely NOT be based on address, since equal objects must have equal hashes. Commented Jun 6, 2012 at 16:18
  • here is a link on how: stackoverflow.com/questions/4930781/… Commented Jun 6, 2012 at 16:19
  • @HotLicks, someone really has to fix the non-sense doc, it's just misleading. Commented Jun 6, 2012 at 16:27
  • Sometimes the nonsense is intentional. Commented Jun 6, 2012 at 17:26

4 Answers 4

7

Not necessarily. From the documentation (emphasis mine):

As much as is reasonably practical, the hashCode method defined by class 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.)

Sign up to request clarification or add additional context in comments.

11 Comments

Interesting to note that hashcode being an int, it is very possible that the address space on a computer could be larger than the number of available int and that the conversion of the internal address into an int is not necessarily a bijection.
"This is typically.." means, are there any other means of implementing it?
@assylias, not necessarily a bijection... but it doesn't matter anyways.
@user601L, actually it has not been (jnit) (void*) address since long long long time, the actual impl. stores a one-time random in the object header.
So but as the doc says, "It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results" is NOT true if the hashcode method hasn't been overridden right?
|
2

You can always check by looking at the source that ships with your JDK.

My java.lang.Object shows hashCode as a native method. Here are the javadocs.

/** * Returns a hash code value for the object. This method is * supported for the benefit of hashtables such as those provided by * <code>java.util.Hashtable</code>. * <p> * The general contract of <code>hashCode</code> is: * <ul> * <li>Whenever it is invoked on the same object more than once during * an execution of a Java application, the <tt>hashCode</tt> method * must consistently return the same integer, provided no information * used in <tt>equals</tt> comparisons on the object is modified. * This integer need not remain consistent from one execution of an * application to another execution of the same application. * <li>If two objects are equal according to the <tt>equals(Object)</tt> * method, then calling the <code>hashCode</code> method on each of * the two objects must produce the same integer result. * <li>It is <em>not</em> required that if two objects are unequal * according to the {@link java.lang.Object#equals(java.lang.Object)} * method, then calling the <tt>hashCode</tt> method on each of the * two objects must produce distinct integer results. However, the * programmer should be aware that producing distinct integer results * for unequal objects may improve the performance of hashtables. * </ul> * <p> * As much as is reasonably practical, the hashCode method defined by * class <tt>Object</tt> 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.) * * @return a hash code value for this object. * @see java.lang.Object#equals(java.lang.Object) * @see java.util.Hashtable */ public native int hashCode(); 

2 Comments

the impl. of the native is in thread.cpp and synchronizer.cpp :: get_next_hash, hg.printk.org/openjdk6-mips/file/tip/hotspot/src/share/vm/… line:270
Can't imagine why this was voted down three years later. There's value in knowing how to look at the Java source. Where do you think the javadocs come from?
1

As the documentation on Object.hashCode() states,

As much as is reasonably practical, the hashCode method defined by class 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.)

So, the Java language has no requirement for the hashcode of the Object class to return the memory address of the object and therefore you should not rely on this.

Comments

1

No, the HashCode() function returns an integer. If you have not defined a HashCode() function for your object, Java MAY transcode the memory address of the object to an integer and return that.

3 Comments

It MAY use the memory address, it does not HAVE to.
Thanks for the correction, updated.
@Robin, it has not been returning addresses since forever

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.