How to write a high quality Equals method

Source: Internet
Author: User
Tags object object

What is the Equals method

Indicates whether another object is equal to this object, the Equals method exists in the object class, the class we write inherits object, and the Equals method of object can be overridden to implement our logic to determine whether two objects are equal.

The Equals method in the object class

Take a look at the source code in the object class

public boolean equals (Object obj) {return
(this = = obj);
}

We can observe a few points:

The Equals method is public decorated and the outer class is accessible

The return value of the Equals method is equal to two objects, and false to two objects Boolean,true

The argument type is object and its subclasses can be compared

The method body determines whether the two references are equal

Why do you actually write equals

In our actual operation, there are the following examples:

@Test public
void Whyuseequals () {
    User User1 = new User ("Xujianguo");
    User User2 = New User ("Xujianguo");
    System.out.println (User1.equals (User2));
}

The name of the User1 and User2 are all xujianguo,age are 21, according to our logic is the same person, is that if you do not rewrite the Equals method, the default is to use the Equals method of object, the result is false, So we have to implement equals to implement our logic.

High-quality Equals method

1. Use the instanceof operator to check whether the parameter is the correct type

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/net/

Because the equals argument is object, your class is person, but this parameter is probably not the person type, probably the user type, so it doesn't fit your judgment, and using instanceof can determine if your argument is the right type of processing you want.

@Override public
    Boolean equals (Object object) {
        if (!) ( Object instanceof) {return
            false;
        } else {
            //...
            return true;
        }
    

2. Convert the parameters to the correct type

After the type judgment is made, it is necessary to convert the type to the desired type before we can perform the subsequent operation.

@Override public
    Boolean equals (Object object) {
        if (!) ( Object instanceof) {return
            false;
        } else {person person
            = (person) object;
            //...
            return true;
        }
    

3. Field conditions in matching parameters

Here is the realization of your logic, how your two objects are equal, for my person class, name as well as age, the two are equal.

@Override public
    Boolean equals (Object object) {
        if (!) ( Object instanceof) {return
            false;
        } else {person person
            = (person) object;
            if (Person.getname (). Equals (name) && person.getage () = = age) {return
                true;
            } else {return
                true;< c10/>}}}
    

4. After writing to complete the Equals method, check its three characteristics: reflexivity, symmetry, transitivity

Reflexivity: X.equals (x) returns true for any Non-null reference value X

Symmetry: For any non-null reference value x, Y, when X.equals (y) returns True, Y.equals (x) returns True

transitivity: X.equals (z) returns true for any Non-null reference value x, Y, Z, when X.equals (y) and y.equals (z) return True

The high quality Equals method must satisfy these three characteristics

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.