hashCode function in Object class.

Hi! to all!I have got a confusion about hashCode function of Object class, which returns an integer.What is the real purpose of introducing this method in Object class.Please comment.
[211 byte] By [Sumit_Tyagia] at [2007-10-3 2:59:10]
# 1

hashCode() method of the object is intorduced for the benefit of collections like HashTables. Typically hashCode method should return distinct integers for distinct objects which are decided distinct based on equals method of the object. Though this is not mandatory , if distinct objects have distinct hashCodes, it will improve the performance of HashTables.

balaji31a at 2007-7-14 20:48:44 > top of Java-index,Java Essentials,Java Programming...
# 2

> hashCode() method of the object is intorduced for the benefit of collections like HashTables. Typically

> hashCode method should return distinct integers for distinct objects which are decided distinct based on

> equals method of the object. Though this is not mandatory , if distinct objects have distinct

> hashCodes, it will improve the performance of HashTables.

A good distribution of hash codes will indeed help in the performance of a HashMap or Hashtable. However, by definition, hashcodes are not necessarily distinct for objects that are distinct based on equals. Two objects for which "equals" is true should have the same hashcode, but two objects which have the same hashcode don't have to have "equals" be true. There is a limited number of hashcodes (the range of int), but an unlimited number of objects. So, some objects will necessarily have the same hashcode. The pigeonhole principle describes this situation:

http://en.wikipedia.org/wiki/Pigeonhole_principle

MLRona at 2007-7-14 20:48:44 > top of Java-index,Java Essentials,Java Programming...