17
4

3 回答 3

18

hashCode is a native method which means that a system library is called internally. See Java Native Interface for more details.

There is a question on SO Why hashCode() and getClass() are native methods? Might be interesting for you.

于 2012-11-28T10:09:28.773 回答
5

The default hashCode is going to be implementation-specific. I suspect it's related to the memory address, but note that the VM moves objects around in memory (and, of course, the hashCode has to remain the same). So it won't be the actual memory address.

于 2012-11-28T10:10:43.343 回答
3

The default hashcode() implementation frequently but not always provides an integer based loosely on the memory address of the object, however the memory address can change. This may vary based loosely upon the JVM implementation.

hashCode()

As you know this method provides the has code of an object. Basically the default implementation of hashCode() provided by Object is derived by mapping the memory address to an integer value. If look into the source of Object class , you will find the following code for the hashCode.

public native int hashCode();

It indicates that hashCode is the native implementation which provides the memory address to a certain extent. However it is possible to override the hashCode method in your implementation class.

http://www.javaworld.com/community/node/1006

于 2012-11-28T10:08:03.637 回答