[Reading notes] Effective Java Chapter III

Source: Internet
Author: User

Follow the general conventions when overriding the Equals methodThe idea is to illustrate that many of the default criteria defined in the object class are standards that are being adhered to by many tool classes or third-party frameworks, and that once such a standard mechanism is changed, it creates an unstable factor in subsequent use. The Equals method is commonly used for the following purposes and is not recommended to overwrite the Equals method: 1. Determine the uniqueness of the instance 2. Provide some degree of logical equality the Equals method satisfies reflexivity, symmetry, transitivity, consistency, non-nullability. When you need to override the Equals method, you need to be aware of the following three points: 1. Always overwrite hashCode2 when overriding equals. Do not attempt to make the Equals method too smart 3. Do not replace object objects in the equals declaration with other typesOverriding the equals method always overwrites HashcodeIn each class that overrides the Equals method, you must also overwrite the Hashcode method. If you do not do this, you violate the general conventions of Object.hashcode, which prevents the class from working properly with all hash-based collections, such as Hashmap,hashset and Hashtable.always overwrite ToStringThis item is mostly intended to provide the necessary critical information when printing data, because the default ToString method only returns the unsigned hexadecimal representation of the class name [email protected]+ hash code.cover clone with careThe purpose of the Cloneable interface is expressed as an object allowed to be cloned (clone). If a class implements the Clone method of Cloneable,object, it returns a domain-wise copy of the object. If you override the Clone method in a non-final class, you should return an object that is obtained by calling Super.clone. If all superclass of a class follows this rule, calling Super.clone eventually calls the Clone method of object. Thus creating the correct instance. If you decide to implement the Clone method with a thread-safe class, remember that its Clone method must be well synchronized. In short, all classes that implement the Cloneable interface should overwrite clone with a public method. This public method first calls Super.clone and then corrects any fields that need to be remediated.consider implementing the comparable interfaceclass implements the comparable interface, it indicates that its instance has an intrinsic ordering relationship.

[Reading notes] Effective Java Chapter III

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.