aspose file tools*
The moose likes Beginning Java and the fly likes object hashCode implementation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "object hashCode implementation" Watch "object hashCode implementation" New topic
Author

object hashCode implementation

Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
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
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

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

Joined: Mar 17, 2011
Posts: 8246
    
  23

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


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
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

Joined: Mar 17, 2011
Posts: 8246
    
  23

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

Joined: Aug 16, 2005
Posts: 14351
    
  22

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39877
    
  28
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

Joined: Mar 26, 2012
Posts: 186

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


The biggest gamble will be to ask a question whose answer you know in that it will challenge your theory | www.TechAspire.blogspot.in
 
 
subject: object hashCode implementation