Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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
1  
Should this be tagged as Java specific? The answer is language dependent. (See blogs.msdn.com/b/csharpfaq/archive/2004/03/29/… 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
show 3 more comments

2 Answers

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
1  
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
add comment

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
add comment

Your Answer

 
discard

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.