Dear friends, I wanred to know when should one override the equals() method.If you override the equals() method, should you do anything else. Thanks in advance, Reagrds, Milan
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
0
Actually every class should override the equals and hashcode methods. This insures that objects of that class will be properly stored in hash and set objects.
I wouldn't say "every class" -- not every class has a meaningful notion of semantic equality between different instances. But if you do override equals(), you MUST override hashCode(), and adhere to the contract: equal objects have equal hash codes (but unequal objects don't necessarily have unequal hash codes) [ October 12, 2002: Message edited by: Ron Newman ]