Effactive Java-A common method for all objects

Source: Internet
Author: User

When overwriting Equb, follow the general conventions:

  1. The reflexive nature. For any non-null reference value X,xequals (x) must return True.
  2. Symmetry. For any non-null reference value x and Y, x.equals (y) must return True when the Y.equals (x) of the document returns True.
  3. Transitivity. For any non-null reference value x, Y and Z, if X.equals (y), and Y.equals (z), then X.equals (z) is true.
  4. Consistency. For non-null reference values x and Y, multiple calls to X.equals (y) return true, or always return false, as long as the information used by the comparison operation on the object is not modified.
  5. Non-nullability. X,x.equals (NULL) must be false for any non-null reference value.

Tips for achieving high-quality equal methods:

  1. Use the = = operator to check if the parameter is a reference to this object.
  2. Use the instanceof operator to check if the parameter is the correct type.
  3. Convert the parameters to the correct type.
  4. For each key field in the class, check to see if the field in the parameter matches the corresponding field in the object.
  5. Determine the symmetry, consistency, and transitivity of the Equals method.
  6. overriding equals always overwrites hashcode.
  7. Don't try to make equals too smart.
  8. Do not replace object objects in the equals declaration with other types.

Effactive Java-A common method for all objects

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.