tags:

views:

38

answers:

3

difference between hash code and reference or address of an object?

A: 

The address of/reference to an object points to the location in memory that an Object lives.

A Hash Code is generated via a hashing algorithm and is used to identify objects in a hash collection. Different algorithms will produce different codes (poor algorithms will result in poor hash codes and more collisions in the collection).

Justin Niessner
A: 

The hash code of an object and it's adress in memory are not related in any way. It's just that different implementations of objects (in different languages, actually) use the memory adress of an object as a unique way of identifying that object.

In general, the method pair hashCode and equals provide the means to compare objects for identity. When implementing your own hashing scheme, you should keep in mind that the hash value should be composed of something that makes the object unique. For example, imagine you provide an object mapper to a database system and want to introduce a Customer object - you know that in your table, customers are unique with respect to their primary key, so returning this primary key as a hash code would be perfectly acceptable and not in any way related to the object's memory adress.

If a customer is identified using his first name, lastname and birth date (which is not really enough to identify a person uniquely, but let's keep it simple for the sake of brevity) then you could hash those 3 values into a hash code and use that in your O/R mapping implementation.

Jim Brissom
A: 

In JavaME, these three things are unrelated.

An object's hashCode is a semi-unique identifier for it.

A reference to an object is a scoped handle on that object.

An object's address is (probably) unobtainable, and certainly useless.

Alison