Chapter 2nd Creating and destroying objects
1. Consider using the static factory method instead of the constructor--static Factory mode.
2. Use the private constructor to harden the singleton property---singleton mode.
3. The ability to harden non-instancing through private constructors:
A. Attempts to force a class to be instantiated by making it an abstract class are not feasible.
B. If the class contains a private constructor for a single display, it cannot be instantiated.
4. Avoid creating duplicate objects.
5. Eliminate expired object references.
6. Avoid using the finalizer function.
Memory leak issues:
If a stack shows growth and then shrinks, then the objects that are popped from the stack will not be garbage collected, even if the client program using the stack no longer references those objects, they will not be recycled. This is because the stack internally maintains an outdated reference to these objects.
Problem FIX:
Once the object reference expires, the references are emptied. (for example, if a unit is ejected from the stack, its reference is set to null). The advantage is that if they are later incorrectly dereferenced, the program throws a null pointer exception immediately, rather than silently running down the wrong way.
Scene:
1. As long as a class manages its own memory.
2. Cache.
3rd. Methods that are common to all objects
7. When rewriting equals, follow the general conventions:
When a class has its own unique "logical equality" concept (rather than whether they point to the same object), and the superclass does not overwrite equals to achieve the desired behavior, we need to rewrite it.
This is usually appropriate for a "value class" scenario, such as an integer or date.
General Conventions:
A. reflexivity; x,x.equals (x) must be true for any reference value.
B. symmetry; x.equals (y) must also return true for arbitrary reference values x and Y, and only if Y.equals (x) returns True.
C. transitivity; if X.equals (y) returns true for any of the reference values X, Y, and Z, and Y.equals (Z) also returns True, then X.equals (z) must also return true.
D. consistency; for arbitrary reference values x and Y, if the object information for the Equals comparison is not modified, then multiple calls to X.equals (Y) either consistently return TRUE or return false consistently.
E. Non-nullability; X,x.equals (null) must return FALSE for any non-null reference value.
A "prescription" for a high-quality equals method:
1. Use the = = operator to check if the argument is a reference to the object;
2. Use the instanceof operator to check if the argument is the correct type;
3. Convert the arguments to the correct type;
4. For each key (significant) field in the class, check that the field in the argument matches the corresponding field value in the current object.
5. When you have finished writing, check that it is symmetrical, transitive, and consistent.
8. When overwriting equals, always overwrite hashcode equal to the object must have equal hash code.
9. Always overwrite ToString. The default is to return the class name [email protected]+16-bit binary hash code.
6th Chapter Method
23. Check the validity of the parameters and do the verification.
24. Use protective copies when needed:
Java is a safe language that is automatically immune to buffer overflows, array overrun, illegal pointers, and other memory corruption errors.
25. Prototype a discreet design approach:
A. Follow the standard command habits.
B. Do not go too far to provide convenient methods, only one operation is used very frequently, to provide a quick method.
C. Avoid a long parameter list, three as the maximum value in practice.
D. For parameter types, the interface is preferred instead of the class. For example, there is no reason to use Hashtable as input when writing a method, instead, map should be used. This allows you to pass in a Hashtable, HASHMAP, treemap Child mapping table. If you use a class instead of an interface, it restricts the customer to passing in only one specific implementation.
E. Use the function object sparingly.
26. Use the overload with caution:
Never export two overloaded methods with the same number of parameters.
27. Returns a value of 0 length instead of NULL.
The 7th Chapter general programming
31. If precise answers are required, avoid using float and double--> as currency calculations.
33. Understand the performance of string connections:
Use the StringBuffer Append method.
34. Referencing objects via interfaces
Declare it as much as possible:
List subs=new ArrayList ();
Rather than such a statement:
ArrayList subs=new ArrayList ();
It is appropriate to use a class to refer to an object if no suitable interface exists.
8th Chapter Exception
40. Use a run-time exception for program errors when using a checked exception for recoverable conditions.
46. Try to keep the failure atomic:
A failed method call should keep the object "in its state before it is called."
47. Do not ignore exceptions:
The catch block should also contain a description that explains why it is appropriate to ignore the exception.
Chapter 9th Threading
51. Do not rely on the thread scheduler (threads Schcduler):
1. Any program that relies on the thread scheduler to achieve correctness or performance requirements is likely to be non-portable.
2. Do not attempt to "fix" the program by calling Thread.yield.
3. Thread priority is the most non-portable feature on the Java platform.
The only use of 4.thread.yield is to artificially increase the concurrency of a program during testing.
53. Avoid using thread groups.
The 10th chapter of serialization
54. Implement serializable with care:
1. The biggest cost to achieve this is that once a class is released, the flexibility to "change the implementation of this class" is greatly reduced.
2. The second price is to increase the likelihood of bugs and security vulnerabilities.
3. The third cost is that with the release of a new version of a class, the associated test burden increases.
55. Consider using a custom serialization format.
56. Write the ReadObject method in a protective manner.
57. Provide a Readresolve method if necessary.
Effective java--Reading notes