Effective Java Reading notes two methods common to all objects

Source: Internet
Author: User

Although object is a specific class, it is designed primarily to extend. All of its non-final methods have explicit general conventions. Any one class must comply with these general conventions when it is override.

One, please comply with the General agreement when you cover equals

1. The default Equals method convention in object is that each instance of a class is equal to itself. When a class has its own unique concept of "logical equality," it should override the Equals method.

2. Timestamp extends the date, and timestamp's equals implementation does violate the symmetry. If the timestamp is used together with the date mix, it may cause incorrect behavior.

3. Do not make the Equals method dependent on unreliable resources.

4. The trick to achieving a high-quality equals method is:

A, use the = = operator to check if the parameter is a reference to this object.

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

c, change the parameters to the correct type.

D, for each key field in the class, check whether the field in the parameter matches the corresponding field in the object.

E, after completing the Equals method, check whether it is symmetric, transitive, and consistent.

Attention:

A, after overriding the Equals method, be sure to overwrite the Hashcode method.

B, do not let the Equals method be too intelligent.

C, do not replace object objects in the equals declaration with other types. This problem can be resolved with @override.

Second, cover equals when always overwrite Hashcode

1. Overwriting the Equals method without overwriting the Hashcode method will result in the class not working properly with the hash-based collection.

2. Objects of equal size must have equal hash codes.

3. The Equals method and the Hashcode method are best used for the same domain. In the calculation of the hash code, adding multiplication makes the hash code is Order-related, more rigorous and reasonable.

4, for the immutable class, the hash code can be cached and Raiga-load optimization.

5. Do not attempt to improve performance by excluding key parts of an object from the hash code calculation.

Third, always overwrite the ToString method

1, the provision of a good tostring method can be used to more comfortable class.

2. The ToString method returns all the interesting information contained in the object.

3, whether to formulate the return value of the format can be determined according to the actual situation.

4. Provides a programmatic way to access all the information that the ToString return value contains.

Iv. overriding the Clone method with care

1. The purpose of the Cloneable interface is to act as an Mixin interface for an object, without any method. It determines the behavior implemented by the protected Clone method in object.

2, the realization of the cloneable class, we always expect it also provides a functional appropriate public clone method.

3. The Clone method is another constructor, and you must ensure that it does not harm the original object and that the constraints in the cloned object are created correctly.

4, copy of the constructor and copy factory is a good way to achieve the object copy. All common collections implement a copy constructor.

5, try not to overwrite the Clone method, also never use it, unless the copy array.

V. Consider the implementation of the comparable interface

1. The Comparato method is not a method in object, but is the only method in the comparable interface. This method can not only compare equivalence, but also make sequential comparisons.

2, the General Convention of the interface is defined by the Equals method, but the ordered set uses the equivalence test of the CompareTo method.

3, if it is a value class, and has a clear intrinsic ordering relationship, because it is determined to implement the interface.

Effective Java Reading notes two methods common to all objects

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.