Valid Java, inclutivejava

Source: Internet
Author: User

Valid Java, inclutivejava

Chapter 4 create and destroy objects

1. Consider replacing the constructor with the static factory method --> static factory mode.

2. Use a private constructor to enhance the singleton attribute --> singleton mode.

3. Strengthen the non-instantiation capability through private constructor:

A. It is not feasible to force a class to be instantiated by making it an abstract class.

B. As long as the class contains a single private constructor, it cannot be instantiated.

4. Avoid creating duplicate objects.

5. Remove expired object references.

6. Avoid using the finalizer function.

 

Memory leakage:

If a stack shows growth and then shrinks, the objects popped up from the stack will not be recycled as garbage, even if the client program using the stack no longer references these objects, they will not be recycled either. This is because the stack maintains expired references to these objects.

Problem fix:

Once the object reference expires, the references are cleared. (For example, when a unit is popped up, its reference is set to null ). The advantage is that, if they are mistakenly removed from the reference later, the program will immediately throw a null pointer exception, rather than quietly running the error.

Scenario:

1. As long as a class manages its memory by itself.

2. cache.

 

Chapter 1 common methods for all objects

7. comply with the general conventions when rewriting equals:

When a class has its own unique "logical equality" concept (rather than whether they point to the same object), and The superclass do not rewrite equals to implement the expected behavior, we need to rewrite it.

This is usually suitable for "value classes", such as Integer or Date.

General Conventions:

A. Self-inverse; for any reference value x, x. equals (x) must be true.

B. Symmetry. For any reference values x and y, and only if y. equals (x) returns true, x. equals (y) must return true.

C. passed; for any reference values 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.

D. consistency. For any reference values x and y, if the object information compared with equals is not modified, the x. equals (y) either returns true consistently or false consistently.

E. Non-null. For any non-null reference values x and x. equals (null), false is returned.

A "recipe" for implementing a high-quality equals method ":

1. Use the = Operator to check whether the real parameter is a reference to the object ";

2. Use the instanceof operator to check whether the real parameter is of the correct type ";

3. Convert real parameters to the correct type;

4. For each "significant" field in the class, check whether the field in the real parameter matches the field value in the current object.

5. After writing, check whether it is symmetric, transmitted, and consistent.

8. When you rewrite equals, objects with the same hashCode must have the same hash code.

9. always rewrite toString. By default, the system returns the class name + @ + 16-bit hash code.

 

Chapter 1 Method

23. Check the parameter validity --> verify.

24. Use protective copy when necessary:

Java is a secure language and automatically immune to buffer overflow, array out-of-bounds, invalid pointers, and other memory damages.

25. prototype of the careful design method:

A. Follow standard command habits.

B. do not provide convenient methods. A quick method is provided only when an operation is frequently used.

C. Avoid a long parameter list. The three values are considered as the maximum values in practice.

D. For parameter types, the interface rather than the class is preferred. For example, there is no reason to use Hashtable as input when writing a method. Instead, Map should be used. This allows you to input a sub- ing table of Hashtable, HashMap, and TreeMap. If you use a class instead of an interface, you can only pass in a specific implementation.

E. Use Function objects with caution.

26. Use overload with caution:

Never export two overload methods with the same number of parameters.

27. Return a zero-length value instead of null.

 

Chapter 2 General Program Design

31. If accurate answers are required, avoid using float and double --> not suitable for currency calculation.

33. Understand the performance of string connection:

Use the append method of StringBuffer.

34. Reference objects through interfaces

Try to declare as follows:

List subs=new ArrayList();

Instead of the following statement:

ArrayList subs=new ArrayList();

If no suitable interface exists, it is appropriate to reference an object using a class.

 

Chapter 2 exceptions

40. Exceptions occur when the recoverable condition usage is checked, and runtime exceptions are used for program errors.

46. Strive to keep failure atomic:

A failed method call should keep the object in the "State before it is called" state ".

47. Do not ignore exceptions:

The catch Block should also contain a description to explain why it is appropriate to ignore this exception.

 

Chapter 3 threads

51. Do not rely on the thread scheduler (thread schcduler ):

1. Any program that relies on the thread scheduler to meet the correctness or performance requirements may not be transplanted.

2. Do not attempt to "correct" the program by calling Thread. yield.

3. The thread priority is the least portable feature on the Java platform.

4. the only purpose of Thread. yield is to artificially increase the concurrency of a program during the test.

53. Avoid using thread groups.

 

Chapter 3 serialization

54. Implement Serializable with caution:

1. The biggest cost for implementing it is that once a class is released, the flexibility of "changing the implementation of this class" will be greatly reduced.

2. The second price is to increase the possibility of bugs and security vulnerabilities.

3. The third price is that, with the release of a new version of a class, the related test burden increases.

55. Consider using a custom serialization form.

56. Write the readObject method in a protective way.

57. Provide a readResolve method if necessary.

 

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.