【发布时间】:2015-01-08 02:04:30
【问题描述】:
AutoCloseable在jdk1.7中引入,Cloesable已经在jdk1.5中。
根据https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
try-with-resources 语句确保每个资源都被关闭 在声明的最后。任何实现的对象 java.lang.AutoCloseable, 包括所有实现的对象 java.io.Closeable,可以作为资源使用。
所以,Closeable 实例已经可以被视为try-with-resources 语句中的资源。这是肯定的,因为Closeable 扩展自AutoCloseable。
我的问题是java为什么专门引入AutoCloseable,为什么不只在try-with-resources支持Closeable,除了try-with-resources还有其他的AutoCloseable使用方式吗?
【问题讨论】:
标签: java autocloseable