tags:

views:

205

answers:

4

Let's say that you have overridden an object's equals() and hashCode() methods, so that they use the object's fields.

How you do you check if two references are to the same object, ala the stock equals() method?

+4  A: 

That's what the == operator does.

Tim Moore
+15  A: 

Use == on objects to perform identity comparison.

That is what the default implementation of equals() does, but one normally overrides equals() to serve as an "equivalent content" check.

joel.neely
Of course! I can't believe I forgot that one!
tunaranch
Also, most equals operations perform the this == obj check, because obviously if they are referencing the same object, then equals will return true (assuming equals works :))
MetroidFan2002
+1  A: 

The default bahaviour of equals() is to compare the two objects using the == operator. So if you want the default bahaviour use ==, if you want your overridden behaviour use equals().

Vincent Ramdhanie
+1  A: 

I'm gonna give the same answer. But here is a link to a good reference. See for yourself.

jjnguy