记录一下最主要学习心得,不然凭我这种辣鸡记忆力分分钟就忘记白看了...
用静态工厂方法代替构造器的最主要好处
1.不必每次都创建新的对象
Boolean.valueOf
Long.valueOf
2.直接返回接口的子类型,对于外界来说并不需要关心实现细节,主要知道这个接口就行
Collections.unmodifiableList
......
为什么避免使用终结方法
1.终结方法不会被及时执行
2.不同jvm上实现不同
3.可能根本不会执行
4.在其中抛出的异常会被忽略
5.性能差
何时使用:
1.作为释放资源的备用方法
FileInputStream:
1 protected void finalize() throws IOException { 2 if ((fd != null) && (fd != FileDescriptor.in)) { 3 4 /* 5 * Finalizer should not release the FileDescriptor if another 6 * stream is still using it. If the user directly invokes 7 * close() then the FileDescriptor is also released. 8 */ 9 runningFinalize.set(Boolean.TRUE); 10 try { 11 close(); 12 } finally { 13 runningFinalize.set(Boolean.FALSE); 14 } 15 } 16 }