Summary of the essentials of effective Java

Source: Internet
Author: User

1. Replace the constructor with a static factory method.

2. Consider using the builder when you encounter multiple parameter constructors.

3. Strengthen the singleton with a private constructor or a U.S. drama type.

4. The ability to harden non-instancing through a private constructor.

5. Avoid creating unnecessary objects.

Try to use string str = "xxx" instead of string str = new string ("xxx");

6. Eliminate expired object references.

7. Avoid using the finalization method.

When the finalization method is called or whether it is called is indeterminate.

8. Overriding equals is subject to the general rules.

Reflexive, symmetric, transitive, consistent, non-nullability (x.equals (NULL) return FALSE) is required.

The timestamp in the Java library extends the date, adding the nanoseconds domain, and the equals implementation violates the symmetry. You cannot use timestamp and date in the same collection, which can cause incorrect behavior.

The trick to raising equals: a. Use the = = operation to check if a parameter is a reference to this object, which is worth doing if the comparison operation can be expensive. B. Use the instanceof operation to check if the parameter is of the correct type. C. Convert the parameter to the correct type. D. Overriding equals is always to overwrite hashcode. E. Do not attempt to make the Equals method too intelligent. F. Do not replace object objects in the equals declaration with other types.

9. Always overwrite equals when hashcode is overwritten.

Summary of the essentials of effective Java

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.