implements Closeable or implements AutoCloseable

后端 未结 6 1221
别那么骄傲
别那么骄傲 2020-12-12 11:48

I\'m in the process of learning Java and I cannot find any good explanation on the implements Closeable and the implements AutoCloseable interfaces

6条回答
  •  执笔经年
    2020-12-12 12:13

    Recently I have read a Java SE 8 Programmer Guide ii Book.

    I found something about the difference between AutoCloseable vs Closeable.

    The AutoCloseable interface was introduced in Java 7. Before that, another interface existed called Closeable. It was similar to what the language designers wanted, with the following exceptions:

    • Closeable restricts the type of exception thrown to IOException.
    • Closeable requires implementations to be idempotent.

    The language designers emphasize backward compatibility. Since changing the existing interface was undesirable, they made a new one called AutoCloseable. This new interface is less strict than Closeable. Since Closeable meets the requirements for AutoCloseable, it started implementing AutoCloseable when the latter was introduced.

提交回复
热议问题