Eighth: Follow the general conventions when overriding equals

Source: Internet
Author: User

= = is physically equal

equals is logically equal

Because instance objects of each class are inherently unique, the use of physical equality (= =) means that an instance can only be equal to its own.

The use of logical equality is (equals) to determine whether an instance is equal to some of the key fields of another instance, thus determining if the two instances are equal.

The implementation of the Equals method of the object class: Logically equal if physically equal.

When do I need to overwrite the Equals method of the object class?

1. Expect instances of this class to be equal only to themselves, without overwriting the Equals method, inheriting only the Equals method of the object class.

We compare the instance objects of this class for equality, whether by using = = or equals actually making physical equality judgments.

2. The superclass of this class already covers the Equals method of the object class, and the class inherits the Equals method of the superclass, which is appropriate for itself, and does not overwrite the Equals method.

For example: The Set implementation class inherits the Equals method of the Abstractset class, and the list implementation class inherits the Equals method of the Abstractlist class, and the map implementation class inherits the Abstractmap class

The Equals method.

3. Determine that the Equals method for this class will never be invoked.

When do I need to overwrite the Equals method of the object class?

When a class needs its own unique "logical equality", for example when we want to have some key fields of two instances equal, we assume that they are logically equal, and that calling the Equals method returns True.

If you do not overwrite the Equals method, you inherit the Equals method of the object class, which actually makes the physical equality judgment that an instance can only be equal to itself.

For example: Some "value classes", such as integer,date,string, and so on, when we call their Equals method, we want to return true if the key fields of the two instances are equal, instead of two instances

Returns True when it is the same instance. And the Equals method of these classes must be overwritten, so that the expected results appear when you are a key to a map collection, or as an element of a set set, because their equals

method is called to determine whether the two instances are logically equal.

However, for some instances of controlled classes, such as singleton classes, enumeration classes, these classes do not need to overwrite the Equals method.

Overwriting the Equals method correctly requires adherence to its general conventions:

1. Reflexivity: x.equals (x) must return True. The instance itself must be logically equal.

2. Symmetry: X.equals (y) and y.equals (x) should return the same result, either True or false.

3. Transitivity: X.equals (y) returns True,y.equals (Z) returns True, then X.equals (z) should return true.

4. Consistency: As long as the comparison of the instance object's key property values have not changed, then no matter how many times the Equals method returns the result should be the same, consistent.

5. For a non-null instance of x, x.equals (NULL) always returns FALSE.

The above five conventions are very important. There are many classes, including all collection classes, that depend on whether the instance object passed to them complies with the equals convention.

Combine all the agreed requirements to get the knack of achieving the high-quality equals method:

1. Use the = = operator to check if the reference to the Parameter object is the same reference as the calling object. If yes, returns TRUE.

2. Use the instanceof operator to check that the parameter object is of the correct type. The type here is correct and generally refers to verifying that the type of the parameter object is the current class. Some cases

You can also compare the other implementation classes of the interfaces implemented by the current class.

3. Convert the Parameter object to the correct type. Because the instanceof type has been checked, type conversions can ensure success.

4. For each "critical" field in the class, check whether the field in the Parameter object matches the corresponding field in the current object.

Returns true if all of the above tests are successful, or false.

For a base type field that is neither float nor double, it can be compared directly using the = = operator, for fields of type float, using the Float.compare method for

Double type of field, using the Double.compare method. For fields of reference types, call the Equals method of the reference type to compare.

When the Equals method of a class is complete, it should be clear: Is it symmetric, transitive, consistent? You should write the test code to test it.

Also bear in mind the caveat:

1. Always overwrite the Hashcode method when overriding the Equals method. (Nineth article)

2. Do not attempt to make the Equals method too intelligent.

3. Do not replace the Parameter object type with any other type. Because the argument type of the Equals method provided by the object class is object, if you change the argument object type to another type,

becomes the overload of the Equals method, not the override of the Equals method. So you can add @override annotations to avoid this error.

Eighth: Follow the general conventions when overriding equals

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.