• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

object hashCode implementation

 
Harshana Dias
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found in object class source the hashCode defined as below,

public native int hashCode();


Where is the implementation for above which should return the hexadecimal value of the memory address
 
Paul Clapham
Sheriff
Posts: 21117
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The implementation of the method is in native code -- that's code which isn't written in Java but in some underlying language which interfaces with the operating system the JVM is designed to use.

And your assertion that the hashCode "should return the hexadecimal value of the memory address" isn't quite correct. What the API documentation actually says is this:

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.


Note that Java doesn't have any methods to access the internal address of an object; that's why native code is required to do that.
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harshana Dias wrote:Where is the implementation for above which should return the hexadecimal value of the memory address

Answer: Dunno and don't care; and neither should you.

The only possible reason I can imagine that you'd want to know this information is that you want to use it in some way; and all I can advise you is: DON'T.
At best it's a bad idea, and at worst it's dangerous.

And, as Paul already pointed out, your assertion of what it "should" do is wrong anyway.

Winston
 
Harshana Dias
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:The implementation of the method is in native code -- that's code which isn't written in Java but in some underlying language which interfaces with the operating system the JVM is designed to use.

And your assertion that the hashCode "should return the hexadecimal value of the memory address" isn't quite correct. What the API documentation actually says is this:



Thank you both of you. If possible can you tell me an example of a "some underlying language"? Suppose i am running on windows xp or 7?

 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Harshana Dias wrote:If possible can you tell me an example of a "some underlying language"?

C

Winston
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15354
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you really want to see the source code, you can find it in the OpenJDK project. It's probably written in C++. Note that this is not an easy project to understand, and certainly something that will be way over your head if you're not a really experienced programmer.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why should you need the Object#hashCode implementation. It is much more useful to know the implementation in your class, which you will of course write yourself.
 
Rajdeep Biswas
Ranch Hand
Posts: 221
1
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its very good to track back and understand the code as much as possible, but one small recommendation: Whenever you see "native", go back to API documentation and start imagining....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic