When to use identity comparison instead of equals?

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?

8

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.

3

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.

3

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!)

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)
        doStuff();
    else if (source == someOtherButton)
        doSomethingElse();
    // ...
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật