Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How hashCode() method in Object class is implemented? [duplicate]

Possible Duplicate:
What’s the implementation of hashCode in java Object?

While I was browsing through the Object class, I found that there is only a declaration of the hashCode() method. Where is the implementation part? If there is no implementation how does the hashCode() method return me a result?

like image 932
Nihal Sharma Avatar asked Jan 25 '13 12:01

Nihal Sharma


3 Answers

It's implemented in the native code. As for implementation, it's a bit more tricky - you can alter default implementation. If you look at the "Open JDK" sources you will see the following options:

-XX:hashCode=n (from 0 to 5).

  • 0 – Park-Miller RNG (default)
  • 1 – function of address and some global state
  • 2 – const 1
  • 3 – sequenatial counter
  • 4 – address of an object
  • 5 – thread specific xor-shift

You can find a detailed implmenetation here: http://hg.openjdk.java.net/jdk7/jdk7/hotspot/file/tip/src/share/vm/runtime/synchronizer.cpp

Consider source code and comments of static inline intptr_t get_next_hash() function.

like image 67
Andrey Taptunov Avatar answered Sep 19 '22 02:09

Andrey Taptunov


The native keyword indicates that it has been implemented in native code (the JVM).

like image 45
akaIDIOT Avatar answered Sep 19 '22 02:09

akaIDIOT


If you see the declaration of hashcode

public native int hashCode();

native in declaration indicates that it is implemented natively in jvm code.

like image 45
MoveFast Avatar answered Sep 19 '22 02:09

MoveFast