Item 4: Enforce noninstantiability with a private constructor
有些类需要防止用户实例化一个类,比如说java.lang.Math 和java.util.Arrays,Attempting to enforce noninstantiability by making a class abstract does not work.A class can be made noninstantiable by including a private constructor:
Item 5: Avoid creating unnecessary objects
String s = new String("stringette"); // DON'T DO THIS!
String s = "stringette"; //DO THIS 每一次运行该代码,JVM都保证只有一个"stringette"实例
1.
2.
On my machine, the original version takes 32,000 ms for 10 million invocations, while the improved version takes 130 ms, which is about 250 times faster.
Changing the declaration of sum from Long to long reduces the runtime from 43 seconds to 6.8 seconds on my machine. The lesson is clear: prefer primitives to boxed primitives, and watch out for unintentional autoboxing.
Item 6: Eliminate obsolete object references
每次执行Stack的pop操作,Stack的大小只是在逻辑上减少了,而Object[]中对pop出来的元素的引用依然存在,所以GC不能将其回收,这样的引用被称作obsolete references,这会导致内存泄露。这样做能够避免上述情况:
但是,Joshua提醒:Nulling out object references should be the exception rather than the norm.Generally speaking, whenever a class manages its own memory, the programmer should be alert for memory leaks.Another common source of memory leaks is caches.A third common source of memory leaks is listeners and other callbacks.
Item 7: Avoid finalizers
1. Finalizers are unpredictable, often dangerous, and generally unnecessary.
2. Never depend on a finalizer to update critical persistent state
3. There is a severe performance penalty for using finalizers.
4. Explicit termination methods are typically used in combination with the try-finally construct to ensure termination.