Saral Saxena wrote:
I just want to know what the default behavior of hashcode() in object class..?As when we define our own user defined collection we overwrite hashcode() to store the object in a particular bucket and to make searching faster..??
The default behavior of the Object class is to create a unique value (or try as much as possible to) for the hash code. This is done because the equals() method of the Object class declares equality only if two instances are the exact same object.
Since your class would probably like a different version of equality, you'll need to change the hashCode() method to match. You don't need to override hashCode() for speed, you need to override it so that you honor the equals()/hashCode() contract (if you override equals()).
Siva Vulchi wrote:By default hashcode() of object class returns integer equivalent of object's internal memory address. So if we dont override hashcode() for collections such as HashMap then each object will have unique hashcode which does not make search faster and hence sole intention of hashing mechanism has been not utilized.
So we should have to override hashcode() in such a way that some of the unequal objects(HashMap keys) return same hashcode.
Sheela Khanna wrote: You must override the default hashCode() method so that the hashcodes for keys are produced from the data members of the key objects.This ensures that the hashcode for a given key is always the same..!!
Siva Vulchi wrote:By default hashcode() of object class returns integer equivalent of object's internal memory address.