记录一下最主要学习心得,不然凭我这种辣鸡记忆力分分钟就忘记白看了...

 

用静态工厂方法代替构造器的最主要好处

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     }
View Code

相关文章:

  • 2021-12-20
  • 2021-08-04
  • 2021-11-17
  • 2021-09-30
  • 2021-12-05
  • 2022-01-08
  • 2021-08-24
  • 2022-12-23
猜你喜欢
  • 2021-12-28
  • 2021-05-14
  • 2021-10-06
  • 2021-11-20
  • 2021-11-17
  • 2021-11-03
相关资源
相似解决方案