Reputation: 1586
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?
Upvotes: 17
Views: 18722
Reputation: 2911
If you need to do this for JUnit Assertion, you can also use Assert.assertSame()
Upvotes: 2
Reputation: 33
use == Operator because it compares with the reference not with the content, if u want to compare with content u can use equals() method.
Upvotes: -1
Reputation: 103145
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().
Upvotes: 2
Reputation: 30943
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.
Upvotes: 38