• 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

overriding equals() and hashCode()

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i just want to know if my understanding of overriding hashCode() and equals() is clear by showing you an example



i.e. in the equals() method i've also add a condition to match the hashCode(), so that when two instance of this class is created and call its equals methods, it doesnt need to also run the hashCode method, as it is done already in the overriding equals method
[ August 20, 2006: Message edited by: Firman Drage ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Objects that are "equal" according to the equals method should return the same hashCode. But this is not something that needs to be tested in the equals method.

In fact, if you happen to have a faulty hashCode method (that returns different hashCodes for otherwise equal objects), then this will just spread that problem into your equals method as well.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic