• 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Overriding the equals ( ) method

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Could someone possibly explain this issue? I’ve read that whenever creating classes that do not have a version of the equals( ) method, you should override the method provided by Object. Is the code below the proper way to do this? Thanks.
class K {
public boolean equals ( ) {
}
public static void main (String [] args) {
K k1 = new K( );
K k2 = new K( );
if (k1.equals(k2)) {
System.out.println (“They equal” ;
}
else {
System.out.println (“Not Equal” ;
}
}
}
 
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No it is not the right way.
You must conform to the exact signature of the method you are overriding:

But don't forget that according to the "hashcode" contract, you also have to override it:


If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result


W.
[ October 09, 2002: Message edited by: Wilfried LAURENT ]
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
fresh from the JavaRanch e-press:
Equals and Hash Code - Introduction
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rather than write "if <a condition> is true then return true, otherwise return false" why not just return <a condition> ? As in:

or even just

 
Ranch Hand
Posts: 399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is important to know how to override equals, hashCode, and how to write compareTo. It is also important to know when to override equals and when not to override equals.
Often the behavior of Object.equals() is exactly what is needed. Override Object.equals() when and only when whether two objects are to be considered equal based on information contained in the objects, or in objects to which they refer. Use Object.equals() when and only each instance of a class is to be considered not equal to any other instance of that class, no matter what information it contains or refers to.
The sticky problem with equals arises when you are subclassing a class that already has an equals method. But you already know what to do about that.
 
Wilfried LAURENT
Ranch Hand
Posts: 269
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To Colin:

and of course if value weren't an int but any Object, use equals instead of == .
W.
 
My first bit of advice is that if you are going to be a mime, you shouldn't talk. Even the tiny ad is nodding:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic