aspose file tools*
The moose likes Beginning Java and the fly likes Confused when overidding equals method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confused when overidding equals method" Watch "Confused when overidding equals method" New topic
Author

Confused when overidding equals method

chris brownn
Greenhorn

Joined: Nov 10, 2010
Posts: 2
I've been looking for the proper way to override the the equals method and I've seen a ton of different solutions. I understand most of what is going on (checking for null, instanceof, etc) but a lot of the examples I've seen do something like this...

Isn't this comparing references instead of values? Is something like the following better practice?


Thanks in advance
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18643
    
    8

Have a look back at those examples which use the "==" version to see if perhaps they are comparing the values of two primitive variables. If that's the case (and often it is when you're implementing an equals() method) then no, it's not comparing two references. Primitives don't have an equals() method.

On the other hand if the example is really comparing reference variables, then you're right, it would usually be better practice to compare the values (via equals) rather than the references (via ==).
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Paul Clapham wrote:
On the other hand if the example is really comparing reference variables, then you're right, it would usually be better practice to compare the values (via equals) rather than the references (via ==).

Paul - Just a query - Does that holds good for only String reference variables or any other reference variables (e.x. those references that refers to custom DTO classes)?

@ Chris - This might be useful - http://www.javaranch.com/journal/2002/10/equalhash.html
chris brownn
Greenhorn

Joined: Nov 10, 2010
Posts: 2
Hmm, so if I understand this correctly...

When using "==" this result is false, correct? I guess I'm getting confused because in my previous example this.a and that.a are different objects so how does an "==" work if it is checking to see if the point to they same reference? Or am I thinking about this the wrong way?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

chris brownn wrote:I've been looking for the proper way to override the the equals method and I've seen a ton of different solutions.


There's no one single right way, but the general approach is:

1. If the references are the same (this == that), then return true immediately.

2. If "that" is not of an appropriate class (including if it's null), return false immediately.

3. Compare field-by-field, or whatever rules make sense for comparing the "contents" of your class.


Isn't this comparing references instead of values? Is something like the following better practice?


That is a bit unusual. However, if "a" is of a class that is known to implement equals() as == (such as an enum, or plain old Object, or java.lang.Class), then it works. Or, though I've never run across this situation myself, maybe the semantics of my class's equality say that two instances are equal only if their "a" fields point to the same object, not just if the two "a" objects pass equals().

And, finally, that can also be a shortcut that leads to slightly more terse and/or readable (by some people's opinion) handling of null checks, but in that case, there would still be an additional equals() call.

(Oh, and finally finally for real, if "a" is a primitive, you have to use ==, as primitives have no equals() method--nor any method.)

(And really triple duper finally forever, maybe the author of that class screwed up.)

In the end, unless you know you have a specific reason to use == for reference member variables, just use equals() (checking for null first if that's a valid value for that field).

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

John Jai wrote:
Paul Clapham wrote:
On the other hand if the example is really comparing reference variables, then you're right, it would usually be better practice to compare the values (via equals) rather than the references (via ==).

Paul - Just a query - Does that holds good for only String reference variables or any other reference variables (e.x. those references that refers to custom DTO classes)?

@ Chris - This might be useful - http://www.javaranch.com/journal/2002/10/equalhash.html


ALWAYS use equals() if you want to see if two objects have equal "contents".

Even for those classes where you know that equals() is implemented as ==, you should use equals(), as it makes it clear what you're trying to accomplish.

Only use == if your semantics are truly dependent on the references being equals--that is, pointing to the same object (or both being null).
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Thanks Jeff...
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Confused when overidding equals method