我注意到 Java 中没有 free
关键字,这让我问为什么。
当我创建一个新对象或数组时,该变量的范围是什么?
我熟悉 C++ 和 C。
最佳答案
在 Java 中,对象的生命周期与局部变量的作用域几乎没有关系。例如
File f = new File("a");
f = new File("b");
// variable f is still in scope but the first object can be cleaned up.
或
private final List<File> files = ...
{
File f = new File("a");
files.add(f);
} // f is out of scope but the object lives on in "files".
关于java - "new"的范围,没有对应的 "free",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12093435/