Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I wonder why would anybody want to use identity comparison for fields in equals, like here (Java syntax):

class C {
    private A a;

    public boolean equals(Object other) {
        // standard boring prelude
        if (other==this) return true;
        if (other==null) return false;
        if (other.getClass() != this.getClass()) return false;
        C c = (C) other;

        // the relevant part
        if (c.a != this.a) return false;

        // more tests... and then
        return true;

    // getter, setters, hashCode, ...

Using == is a bit faster than equals and a bit shorter (due to no need for null tests), too, but in what cases (if any) you'd say it's really better to use == for fields inside equals?

share|improve this question
Should this be tagged as Java specific? The answer is language dependent. (See… for details about C#...) – tugs Oct 5 '12 at 21:22
@tugs: To me it looks now like C# has two things roughly equivalent to Java's equals, whereby one of the doesn't really work (due to missing overloading). My question is primarily about identity (pointer) comparison vs. content comparison, so it's not that Java specific. I know virtually nothing about C#, but I'm pretty sure the question applies to C++ as well. – maaartinus Oct 5 '12 at 21:53
@tugs: Now I'm imagining /b/ on MSDN. – Mason Wheeler Oct 5 '12 at 21:53
This is definitely language-specific. For example, Java does some optimization tricks with strings, to avoid keeping redundant copies in memory; as a result, you can wind up with a situation where you think you instantiated two strings, but they're actually represented by the same object. And C++ has operator overloading, which completely changes everything. – Mike Baranczak Oct 5 '12 at 23:17
@Mike Baranczak: What Java does to Strings doesn't matter in the context of my question as I meant it (which doesn't match well what I wrote). In C++ virtual operator== gets used in place of java's equals and pointer comparison gets used in place of java's ==, so I see there more or less 1:1 correspondence. But feel free to add the tag if you're confident it belongs there. – maaartinus Oct 5 '12 at 23:54

There are cases when checking for instance equality with == is necessary. For example, that is what you do in the implementation of equals to check if you are comparing an object to itself (a very important shortcut that also satisfies the reflexivity requirement of equals)

if (other==this) return true;

The other case is when the system guarantees that there will be only one instance that is equal to any object of a particular class (and by necessity that is going to be the instance itself - again, due to reflexivity of equals). This is another common case illustrated by your code fragment:

if (other.getClass() != this.getClass())

An equality of two java.lang.Class objects is equivalent to the equality of their references, because for each class that you define there is one, and only one, instance of java.lang.Class.

A third case where reference equality is desired is serialization: you need to keep track of object instances by tracking their references, even if they are logically equal, in order to be able to produce an isomorphic graph upon deserialization.

EDIT : Inside implementations of equals the use of == boils down to increasing performance: comparing an object to itself is a common case, especially when you use objects of the class as keys in a hash table, so it makes sense to optimize it.

share|improve this answer
I see that my question isn't very clear (and I still don't know how to make it better). Your first two cases are just optimizations; I could have left other==this out and I could have used equals for the class comparison (as it's actually the same). The serialization case is an important one. But when would you inside equals compare two fields of an object via == for a reason other than performance? – maaartinus Oct 5 '12 at 22:02
@maaartinus Ah, I see what you mean after the edit. Please see the update to the answer. – dasblinkenlight Oct 5 '12 at 22:14
Two more situations where == is appropriate are when an object (such as IdentityHashMap) has received objects from outside code which expects them to be compared with ==, or when a method acts upon two objects, at least one of which is received by outside code and the other of which has been exposed to outside code, and it needs to ensure that in fact has two distinct objects, rather than two references to the same one. – supercat Oct 28 '14 at 18:35

The == operator in Java checks for reference equality: it returns true if the pointers are the same. It does not check for contents equality.

Use the equals() method to compare object values. The equals() method returns a boolean value.

The two operators that can be used with object references are comparing for equality (==) and inequality (!=). These operators compare two values to see if they refer to the same object. Although this comparison is very fast, it is often not what you want. Usually you want to know if the objects have the same value, and not whether two objects are a reference to the same object. For example,

if (name == "My Test Value")   // Legal, but ALMOST SURELY WRONG

This is true only if name is a reference to the same object that "My Test Value" refers to. This will be false if the String in name was read from input or computed (by putting strings together or taking the substring), even though name really does have exactly those characters in it.

Many classes (eg, String) define the equals() method to compare the values of objects.

share|improve this answer
It appears OP already knows that, and in any case this does not address the question which was asked. -1 – delnan Oct 5 '12 at 21:05
@delnan, i did not finish my answer. Now it is more or less completed. – Yusubov Oct 5 '12 at 21:10
Fair enough, downvote removed. Though I do find it strange to "not finish an answer" before posting it. – delnan Oct 5 '12 at 22:10

It is true that very often we use == instead of equals() only for performance, where calling equals() would do just as well, but there exist cases where using == is necessary.

For example, if you have a list which allows duplicate values, (multiple different instances that appear equal according to their equals() methods,) and you want to remove a particular instance from that list, you cannot use equals() to find the instance to remove, because equals() would find you the first duplicate, not the exact instance that you are looking for. To find the exact instance, you must use ==.

Of course, this is just a simplistic example. In order to find out more about why both == and equals() are absolutely necessary, you need to read up on the concept of value semantics vs. reference semantics. (I searched for some good source of information on the subject but could not find anything well written; if someone finds something good, please post!)

share|improve this answer

It's indeed rare when you want to do reference (==) equality instead of logical (equals()) equality. Often when you find yourself checking for reference equality, there might be something wrong with your design, or you might want to implement the equals() method.

Nevertheless, there is one case where I often check for reference equality (not sure if that's good practice):

public void actionPerformed(ActionEvent e){
    AbstractButton source = (AbstractButton) e.getSource();

    if (source == someButton)
    else if (source == someOtherButton)
    // ...
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.