5

Specifically when they're used with a moving garbage collectors. The current memory location of the object can't be used because it can change at the next collection, so what is used as the objects hash?

Daniel Powell
  • 8,143
  • 11
  • 61
  • 108
Alex Gaynor
  • 14,353
  • 9
  • 63
  • 113
  • 2
    The question should be: *How do VMs implement `System.identityHashCode()`*, as this is what is used by the IdentityHashMap. – Paŭlo Ebermann Jun 27 '11 at 01:32
  • possible duplicate of [How does the JVM ensure that System.identityHashCode() will never change?](http://stackoverflow.com/questions/1063068/how-does-the-jvm-ensure-that-system-identityhashcode-will-never-change) – Paŭlo Ebermann Jun 29 '11 at 23:10

2 Answers2

5

Hope this question helps: How does the JVM ensure that System.identityHashCode() will never change?

Also http://xiao-feng.blogspot.com/2007/04/object-hashcode-implementation.html

Community
  • 1
  • 1
ata
  • 8,853
  • 8
  • 42
  • 68
0

As Paulo pointed out in his comment, System.identityHashCode() is used for this. And from the javadocs:

Returns the same hash code for the given object as would be returned by the default method hashCode(), whether or not the given object's class overrides hashCode(). The hash code for the null reference is zero.

So, really, the question boils down to "What is the default hashCode algorithm?

jpm
  • 3,165
  • 17
  • 24