I understand the different between these two terms and what methods you would use if you wanted to check if two objects had the references or the same value. My question is, when would you ever have to check if two objects have the same reference as opposed to checking if two objects have the same content or value(There has never been a time where I've had to check if two objects have the reference)?
-
2I think the question you want to ask is: "When is it appropriate to use reference equality vs object equality in Java?" – Andrew Eisenberg May 15 '15 at 04:20
-
@AndrewEisenberg Yes that it's better and more to the point. – Luis Averhoff May 15 '15 at 04:21
-
1As such Reference equality is a part of Object equality, no need to check further if references point to the same Object, saves some energy – Nitin Dandriyal May 15 '15 at 04:23
-
Now that I think of, neither have I truly needed `==` operator for object comparisons. If java supported operator overloading that would be different.. – Menelaos Kotsollaris May 15 '15 at 04:43
4 Answers
That's not really "instead of" the equality check, but you could do the reference check before you do the equality check for performance and null-safety reasons.
In effect, this is what happens when you call Objects.equals(a,b)
instead of a.equals(b)
.
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}

- 257,207
- 101
- 511
- 656
Well a number of situations arise from the top of my head. First is just in debugging to make sure you are not creating multiple instances or adding the same content to more than one variable, second might be in game design/game modding (Minecraft for example) to look if its the same block being placed somewhere, third is when working with api's, can be used a lot there. Besides even if you don't ever use this feature it's always nice to know you have it:).

- 1,470
- 14
- 22
-
I know, I just wanted to read examples of where it would be appropriate. Yours seem pretty good from what I'm reading. – Luis Averhoff May 15 '15 at 04:23
There has never been a time where I've had to check if two objects have the reference
That means you have never Overridden an equals()
method (or properly).
Reference equality is a part of Object equality, In case both the references being compared point to the same Object, you reduce further comparison just by using == and not comparing further if true.
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
--
--

- 1,559
- 11
- 20
-
You don't have to implement it like that, though. If the references are equal, all the following checks will also yield equality. So the result will be the same. – Thilo May 15 '15 at 05:12
-
Your point is right. You need the ==
operator for primitive comparing most of the times. There would be an another case where you could compare the references of your objects, say that you have shuffled your list and you want to get a specific object, so you would need to check your objects references, but that's a trivial case in general.
Imagine now a world where Java supported operator overloading. The answer to your question would have been totally different. Let me remind you why Java does not support operator overloading:
I left out operator overloading as a fairly personal choice because I had seen too many people abuse it in C++.
James Gosling. Source: http://www.gotw.ca/publications/c_family_interview.htm

- 1
- 1

- 5,776
- 9
- 54
- 68