top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is difference between hashcode and memory address of an object?

+2 votes
What is difference between hashcode and memory address of an object?
posted Mar 30, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

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.

Example: 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.

answer Mar 30, 2015 by Amit Kumar Pandey