Adrian
Adrian

Reputation: 5681

how is hashCode() implemented in Java

How is hashCode() implemented?

My assumption is that it uses the object memory location as the initial number (the seed) on which it runs the hash function. However, this is not the case.

I've also looked at Hash : How does it work internally? but it does not answer my question.

Yes I could download the SDK, but before I do that and look at the code, perhaps someone else already has knowledge of it.

Thanks :)

EDIT: I know it should be overridden and such, so please try to stay on topic :)

Upvotes: 7

Views: 5214

Answers (5)

G. Demecki
G. Demecki

Reputation: 10596

No, no, no. All answers in this thread are wrong or at least only partially correct.

First: Object.hashCode() is a native method, so its implementation depends solely on the JVM. It may vary between HotSpot and other VM implementations like JRockit or IBM J9.

If you are asking:

how is hashCode() implemented in Java?

Then the answer is: it depends on which VM you are using.

Assuming that you are using Oracle's default JVM—which is HotSpot, then I can tell you that HotSpot has six hashCode() implementations. You can choose it using the -XX:hashCode=n flag running JVM via command line, where n can be:

0 – Park-Miller RNG (default)
1 – f(address, global_statement)
2 – constant 1
3 – Serial counter
4 – Object address
5 – Thread-local Xorshift

The above is copied from this post.

And if you dig a little bit around in the HotSpot source code, you may find below snippet:

if (hashCode == 0) {
  value = os::random();
} else {
  ...

os::random() is just the implementation of the Park-Miller Pseudo Random Generator algorithm.

That's all. There isn't any notion of memory address. Although two other implementations, 1 and 4, use an object's memory address, the default one doesn't use it.
The notion that Object.hashCode() is based on the object's address is largely a historic artefact - it is no longer true.


I know that inside Object#hashCode() JavaDoc we can read:

(...) this is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java™ programming language.

But it is obsolete and misleading.

Upvotes: 25

hburde
hburde

Reputation: 1441

The hashCode method defined by class Object returns distinct integers for distinct objects. This could be implemented by converting the internal address of the object into an integer (but this implementation style is not required by the standard). It gets interesting with new classes which override hashCode in order to support hash tables (equal and hashCode): http://www.javapractices.com/topic/TopicAction.do?Id=28

Upvotes: 0

lrAndroid
lrAndroid

Reputation: 2854

The implementation of the hashcode() function varies from Object to Object. If you want to know how a specific class implements hashcode(), you'll have to look it up for that class.

Upvotes: 0

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

Of course it is implementation specific, but generally the hash code for an object will be computed lazily and stored in the object header. Odd things are done with headers to keep them small whilst allowing complex locking algorithms.

In the OpenJDK/Oracle JVM the usual method of computing the initial hash code is based on the memory address at the time of the first request. Objects move about in memory, so using the address each time would not be a good choice. The hash code isn't the actual address - that would typically be a multiple of eight which isn't great for using straight in a hash table particularly with a power of two size. Note identity hash codes are not unique.

HotSpot has build time options to always use zero or use a secure random number generator (SRNG) for testing purposes.

Upvotes: 5

Taymon
Taymon

Reputation: 25676

I'm assuming you're talking about the Object implementation of hashCode, since the method can and should be overridden.

It's implementation dependent. For the Sun JDK, it's based on the object's memory address.

Upvotes: -1

Related Questions