Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals()

 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


when we don't override eqauls() and hashCode() methods,Object.equlas() gives false and hashcode() may be true or false value returning.
But i don't get the part why do we need to override those two method what is the wrong happening here?

please guide me on those.
[ April 14, 2007: Message edited by: Shiva Mohan ]
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But i don't get the part why do we need to override those two method what is the wrong happening here?


Well, you don't have to override those two methods.

The default equals() method (from Object class) will return the value of true, if two references are referring to the same instance. The default hashCode() method will use the identity hash as the hashcode. This hashcode will very likely be different for different instances, but may not be as there can only be so many different values for hashcode.

If this behavior is fine for your purposes, then there is no need to override those two methods.

Henry
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply Henry.

if the behavior is not fine for purposes, (Actually i have no idea when we really need to override those two methods regarding hashtable)
 
Henry Wong
author
Marshal
Pie
Posts: 21514
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shiva Mohan:
if the behavior is not fine for purposes, (Actually i have no idea when we really need to override those two methods regarding hashtable)


Hate to point out the obvious but it's instances of your class, designed for your program. It's your decision whether two instances of your class are equal or not. There is no simple answer as it is a design issue -- you have to decide how instances of your class should be treated, and whether the default methods is what you want.

Henry
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic