• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

equals()

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic