If you are going to use an Object as a key in a hashtable you need to override both equals and hashcode.
If you don't override hashcode the hashtable will not be able to find the key unless you keep that object around (which defeats the purpose of haveing a hashtable) because the hashtable will look for the key by hashing the hashcode first. Once it finds the right key location there can be several Object keys in that location so it compares the Objects with the equals method. If they are not equals() it will not find it. If you don't override equals() then equals() just tests ==. (I hope that made sense)
Anytime you override equals() you should always override hashcode and visaversa.
Joined: Oct 11, 2004
It is mentioned in K&B that : If we pass an object reference in SOP then it prints the class name + @ + hash code . But look at this code :
why this two hash codes are different for the same object .
please help . thanks .
For first question can any body put some code . thanks .
Joined: Dec 29, 2004
I didn't test this, but I think they are the same number, just one is in binary and one is in hex.