I
I
Igor Petrov2011-09-06 20:33:12
Java
Igor Petrov, 2011-09-06 20:33:12

implementation of hashCode(); in class Object (Java)

Good evening everyone!

I wanted to ask where you can find the implementation

public native int hashCode();
from class Object? The platform doesn't matter. It just became interesting, but googling really did not give anything.

Thank you!

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
antalus, 2011-09-06
@KriegeR

The OpenJDK source code can be downloaded from openjdk.java.net/
People have already asked this question blogs.tedneward.com/CommentView ,guid,eca26c5e-307c-4b7c-931b-2eaf5b176e98.aspx, I think this is the right direction.

P
png, 2011-09-06
@png

Look at the method definition. There the keyword native costs. This means that the implementation is written in C.
And most likely depends on the execution platform.
There is a JNI mechanism for this.
Well, I will give a link to the official documentation.
For myself, I understand that for a simple Object, the address of the object reference in memory is encoded in hashCode.

K
kk86, 2011-09-06
@kk86

Here I found not the implementation itself, but an explanation of its principle:

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (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.)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question