Friday, September 10, 2010

FYI

If you dont overridea class's equals() method,you wont be able to use those objects as a key in hashtable
equals() method in class Object uses only the == operator for comparisions.So Unless You override equals(), two Objects are considered equal only if two references refer to the same Object

4 comments:

  1. can u please give a demonstration of the above explanation via code, that would be more helpful in understand the concepts more quickly

    ReplyDelete
  2. Sorry guys for the late replies. hashcode() can be thought of for instance ur name itself..the moment you guys were born you were given a name right...

    so lets consider this case you were born as humans so equal method had to be overidden , just to prove that you are equal to human...now ones Equal method is overriden you also need to override hashcode to give u (object) some name which isnt guarnetted unique for instance Soniya can be name for multiple people.

    PS: hashCode() only returns int value.So any object in a collection has a hashcode() which is not unique

    ReplyDelete

Locations of visitors to this page