3

In Java, is an Object's hashcode value generated by algorithm according to the content of the object or according to the object instance memory address?

Jason
  • 11,744
  • 3
  • 42
  • 46
wanghao
  • 271
  • 1
  • 7
  • 21

3 Answers3

1

If you go to the Object class of Java source code it is implemented as:

public native int hashCode();

The native hashCode method implementation depends on the JVM. By default in HotSpot it returns random number based on objects initial memory location, you can check it in the source code (function get_next_hash).

As per the Java docs for this method

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.)

But it is always JVM dependent how it implements hashCode method.

Native method implementation in hotspot jvm to generate hashcode:

static inline intptr_t get_next_hash(Thread * Self, oop obj) {
  intptr_t value = 0 ;
  if (hashCode == 0) {
     // This form uses an unguarded global Park-Miller RNG,
     // so it's possible for two threads to race and generate the same RNG.
     // On MP system we'll have lots of RW access to a global, so the
     // mechanism induces lots of coherency traffic.
     value = os::random() ;
  } else
  if (hashCode == 1) {
     // This variation has the property of being stable (idempotent)
     // between STW operations.  This can be useful in some of the 1-0
     // synchronization schemes.
     intptr_t addrBits = intptr_t(obj) >> 3 ;
     value = addrBits ^ (addrBits >> 5) ^ GVars.stwRandom ;
  } else
  if (hashCode == 2) {
     value = 1 ;            // for sensitivity testing
  } else
  if (hashCode == 3) {
     value = ++GVars.hcSequence ;
  } else
  if (hashCode == 4) {
     value = intptr_t(obj) ;
  } else {
     // Marsaglia's xor-shift scheme with thread-specific state
     // This is probably the best overall implementation -- we'll
     // likely make this the default in future releases.
     unsigned t = Self->_hashStateX ;
     t ^= (t << 11) ;
     Self->_hashStateX = Self->_hashStateY ;
     Self->_hashStateY = Self->_hashStateZ ;
     Self->_hashStateZ = Self->_hashStateW ;
     unsigned v = Self->_hashStateW ;
     v = (v ^ (v >> 19)) ^ (t ^ (t >> 8)) ;
     Self->_hashStateW = v ;
     value = v ;
  }
Abhijeet Dhumal
  • 1,799
  • 13
  • 24
  • you means the hashcode of object is random integer number. – wanghao Sep 08 '15 at 06:29
  • Many people will claim that Object.hashCode will return the address of the object representation in memory. In modern implementations objects actually move within memory. Instead an area of the object header is used to store the value, which may be lazily derived from the memory address at the time that the value is first requested. But this implementation is actually dependent on JVM. So it may differ with each JVM. – Abhijeet Dhumal Sep 08 '15 at 06:35
  • What I believe is the random number is generated from memory location only and it is used further to locate it. I have updated the answer with proper Javadoc reference for more information. – Abhijeet Dhumal Sep 08 '15 at 06:42
0

Custom hashcodes should be generated from the content of an object, not the memory location where it is stored.

You can generate a hashcode for your object any way you like. However, there are some rules:

  • Any two objects that are 'equal' to each other, using .equals(), must produce the same hashcode value.
  • However, the same hashcode value can be generated by two objects that are not equal.
Jason
  • 11,744
  • 3
  • 42
  • 46
  • According to your answer, I can judge whether the object's hashcode is generated by the object content – wanghao Sep 08 '15 at 06:11
  • If you are writing the class, you can generate the hashcode however you want. But to ensure that other classes work correctly in conjunction with yours, you need to adhere to the rules noted above. – Jason Sep 09 '15 at 00:39
0

The default implementation of the hashCode() method returns an integer based on the object's identity and is not based on the values of instance (and class) variables of the object. No matter how many times the values of its instance variables (data fields) change, the hash code calculated by the default hashCode implementation does not change during the life of the object.

You can find more details: HERE and HERE

Nomesh DeSilva
  • 1,649
  • 4
  • 25
  • 43