Follow the general conventions when Java objects overwrite equals

Source: Internet
Author: User

  If any of the following conditions are met, the desired result is:

  Each instance of a class is inherently unique.

Does not care if the class provides a "logical equality" test function.

The superclass has overridden equals, and the behavior inherited from the superclass is also appropriate for subclasses.

Class is private or package-level private, it can be determined that its Equals method is never called.

If a class has its own unique "logical equality" concept (unlike the concept of object equivalence), and the superclass does not overwrite equals to achieve the desired behavior, then we need to overwrite the Equals method. This usually belongs to the "value class" scenario. A value class is simply a class that represents a value, such as an integer or date. When you compare a reference to a value object by using the Equals method, you want to know whether they are logically equal, rather than whether they point to the same object.

  The class that ensures that "there is at most one object per value" does not need to overwrite the Equals method. For such a class, the same logic is the same as the object, and the Equals method of object is equivalent to the Equals method in the logical sense.

The Equals method implements the equivalence relationship:

Reflexivity: X,x.equals (x) must return true for any non-null reference value.

Symmetry: For any non-null reference value x and Y, x.equals (y) must return true only if Y.equals (x) returns True.

Transitivity: For any non-null reference value x, Y, and Z, if X.equals (y) returns True, and Y.equals (Z) also returns True, then X.equals (z) must also return true.

Consistency: For any non-null reference value x and Y, as long as the information used by the comparison operation of equals in the object has not been modified, multiple calls to X.equals (Y) will consistently return true, or return false consistently.

For any non-null reference value, x,x.equals (NULL) must return FALSE.

You cannot expand a class that can be instantiated while adding a new value component while preserving the equals convention. Adding a new value component to a subclass of an abstract class does not violate the Equals convention, because an instance of an abstract class cannot be created.

If the first operand of instanceof is null, the instanceof operator returns false, regardless of the type of the second operand.

Tips for implementing the high-quality equals method:

Use the = = operator to check if the parameter is a reference to this object.

Use the instanceof operator to check if the parameter is the correct type.

Convert the parameters to the correct type.

For each critical field in the class, check that the field in the parameter matches the corresponding field in the object.

  

For a base type field that is neither float nor double, you can use the = = operator to compare, and for the object reference field, you can call the Equals method recursively, and for the float domain, You can use the Float.compare method, or the Double.compare method for a double field. Special handling of float and double fields is due to the existence of Float.nan, -0.0f, and similar double constants. For array fields, apply these rules to each element. If each element in the array field is important, you can use one of the new arrays.equals methods in release version 1.5.

The final recommendation:

Always overwrite hashcode when you overwrite equals.

Do not attempt to make the Equals method too intelligent.

Do not replace the Parameter object objects in the Equals method declaration with other types. Because the method is not overridden after the substitution, it is overloaded.

Resources

"Effective Java Chinese Version 2nd edition" 8th: When overwriting equals, please abide by the General Convention p28-38

Follow the general conventions when Java objects overwrite 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.