How is Object.hashCode() computed?

A

Aryeh.Friedman

Is hash code just a reference to some memory location, i.e. the same
thing as a ptr in C/C++ or is it an actual "fingerprint" of the
instance.

The reason for asking is I am writting a hash table based DB and want
to know if I can relie on Object.hashCode() to be consitent across
process instances and machines.
 
W

Wibble

Hash codes arent guaranteed to be unique. You cant
use them as an object id. What does it mean for them
to be consistent across machines? Its a different
instance when its in a different vm.
 
A

Adam Maass

Is hash code just a reference to some memory location, i.e. the same
thing as a ptr in C/C++ or is it an actual "fingerprint" of the
instance.

The reason for asking is I am writting a hash table based DB and want
to know if I can relie on Object.hashCode() to be consitent across
process instances and machines.

In general, Object.hashCode() is completely opaque -- one VM may use one
algorithm for it, another a different algorithm.

Several well-known VMs use the memory location of the object for this value.
(It's inexpensive to compute and satisfies the contract of hashCode().) Note
that modern VMs can relocate objects, though: so it isn't necessarily a
memory location.

The docs on Object.hashCode say:

public int hashCode()Returns a hash code value for the object. This method
is supported for the benefit of hashtables such as those provided by
java.util.Hashtable.
The general contract of hashCode is:

a.. Whenever it is invoked on the same object more than once during an
execution of a Java application, the hashCode method must consistently
return the same integer, provided no information used in equals comparisons
on the object is modified. This integer need not remain consistent from one
execution of an application to another execution of the same application.
b.. If two objects are equal according to the equals(Object) method,
then calling the hashCode method on each of the two objects must produce the
same integer result.
c.. It is not required that if two objects are unequal according to the
equals(java.lang.Object) method, then calling the hashCode method on each of
the two objects must produce distinct integer results. However, the
programmer should be aware that producing distinct integer results for
unequal objects may improve the performance of hashtables.
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top