Forums Register Login

Confused when overidding equals method

+Pie Number of slices to send: Send
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
1
+Pie Number of slices to send: Send
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 ==).
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
 

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).

2
+Pie Number of slices to send: Send
 

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).
+Pie Number of slices to send: Send
Thanks Jeff...
WARNING! Do not activate jet boots indoors or you will see a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 773 times.
Similar Threads
find method in Data class
infinite recursion with equals
Why is this HashSet allowing duplicates?
How To Get Rid Of java.util.ConcurrentModificationException?
need help plss
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:38:38.