Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

equals()

 
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 ]
 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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
 
I have always wanted to have a neighbor just like you - Fred Rogers. Tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic