• Post Reply Bookmark Topic Watch Topic
  • New Topic

Hash code problem  RSS feed

 
vipul bondugula
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ranchers,

I just want to know how a hash code is generated for an object. i executed a sample program by printing hash code value each time. It is varying.




First time when i executed the value is 11394033
2nd time: 1671711
3rd time: 1671711
4th time: 11394033


sometimes the hash code value is same and also it is varying....

Please tell me how hash code value is calculated for an object.


Thank you
Vipul Kumar
 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hashCode is method of Object class.

You can see the code of hashCode method to understand how hashCode is calculated.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vipul bondugula wrote:
Please tell me how hash code value is calculated for an object


It's not specified. It's up to the implementation. Each JVM might do it a little differently.

However, if you read the docs for hashCode(), you'll see this (emphasis mine):
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 hashCode() value you're seeing is probably derived from the Object's address, and since the JVM, and hence your Java program, can be loaded at different spots in memory each time you run it, it's not surprising that the hashCode changes from one run to the next.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anayonkar Shivalkar wrote:hashCode is method of Object class.

You can see the code of hashCode method to understand how hashCode is calculated.


Yeah, but you have to look at the JVM source code. Object.java in src.zip only has:

 
vipul bondugula
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:

So the hashCode() value you're seeing is probably derived from the Object's address, and since the JVM, and hence your Java program, can be loaded at different spots in memory each time you run it, it's not surprising that the hashCode changes from one run to the next.


Thanks jeff verdegan....

hashCode does not have implementation. At first i am annoyed. After your reply, i got understood...Thank you.
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The hashCode() method does have an implementation. Try thisThe %h tag displays the argument’s hash code. You get an output, ∴ there is an implementation. If you find Effective Java by Joshua Bloch, chapter 3, you will find suggestions about hash codes. Note that has changed slightly since that edition was written.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You get an output, ∴ there is an implementation.


For that matter, this line compiles, ∴ there is an implementation:

 
Anayonkar Shivalkar
Bartender
Posts: 1558
5
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff Verdegan wrote:
Anayonkar Shivalkar wrote:hashCode is method of Object class.

You can see the code of hashCode method to understand how hashCode is calculated.


Yeah, but you have to look at the JVM source code. Object.java in src.zip only has:


I should've confirmed that. My apologies.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!