正在将 Closeable 的新实例分配给之前创建的实例,关闭之前的实例吗?

Is allocating new instance of Closeable to previously created instance, closing previous?

例如Scannerclass,它实现了Closeable接口:

Scanner sc = new Scanner(...);
sc.//do smthing
sc = new Scanner(...); <- is previous Scanner closed? Does it create memory leak?
sc.//do smthing
sc.close();

不,除非您显式调用 sc.close(),或将其范围限定在 try-with-resources block, there are no guarantees it will be closed. Some classes might have a finalizer or an associated Cleaner,但这些非常不可靠(例如,您永远不知道它们何时或什至是否会被调用)。