Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the implementation of hashCode in java Object? [duplicate]

Possible Duplicate:
How is hashCode() calculated in Java

I found there's no implementation in hashCode() method of root class Object in Java:

public native int hashCode(); 

If I have an Object a and an Object b, how can I know the a.hashCode() and b.hashCode() value without using System.out.println()? Just by the hashCode implementation.

I have try to new two ArrayList objects and to my big surprise the hashCode() values are the same: both of them are 1.

like image 445
andyqee Avatar asked Nov 28 '12 10:11

andyqee


People also ask

How is the hashCode implemented in object Java?

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();

What is the default implementation of hashCode in Java?

In Java, hashCode() by default is a native method, which means that the method has a modifier 'native', when it is implemented directly in the native code in the JVM. Used to digest all the data stored in an instance of the class into a single hash value i.e., a 32-bit signed integer.

Can 2 objects have same hashCode?

1) If two objects are equal (i.e. the equals() method returns true), they must have the same hashcode. 2) If the hashCode() method is called multiple times on the same object, it must return the same result every time. 3) Two different objects can have the same hash code.

Can hashCode be duplicated?

No, it will not allow duplicates. hashTable. put(key, value); If key is already present, existing value will be overridden by the new value for a given key and returns the old value.


3 Answers

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.

like image 99
Kai Avatar answered Sep 21 '22 13:09

Kai


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.

like image 42
Brian Agnew Avatar answered Sep 23 '22 13:09

Brian Agnew


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

like image 37
Kevin Bowersox Avatar answered Sep 21 '22 13:09

Kevin Bowersox