Are resources used by a Java application freed when it terminates?

前端 未结 2 1254
礼貌的吻别
礼貌的吻别 2021-01-22 22:38

Java applications may use IO streams, sockets or database connections that should be closed when they are no longer needed.

However, applications may be terminated (e.g.

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-22 23:04

    The JVM will release all active resources upon termination; however, this does not ensure that the other end will free the resource too, so explicitly closing resources is in every programmer's best interest.

    An alternative to explicitly closing streams exists in Java 7, called the try-resource "statement", which is the equivalent of closing a resource in a finally block after a try block. More info on that can be found here.

提交回复
热议问题