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
,为什么不只支持Closeable,try-with-resources
除了AutoCloseable还有其他使用方式try-with-resources
吗?