编程语言
首页 > 编程语言> > Java如何手动删除对象

Java如何手动删除对象

作者:互联网

Java如何像C++一样删除对象

You should remove the references to it by assigning null or leaving the block where it was declared. After that, it will be automatically deleted by the garbage collector (not immediately, but eventually).

Example 1:

Object a = new Object();
a = null; // after this, if there is no reference to the object,
          // it will be deleted by the garbage collector

Example 2:

if (something) {
    Object o = new Object(); 
} // as you leave the block, the reference is deleted.
  // Later on, the garbage collector will delete the object itself.

标签:手动,Java,删除,garbage,deleted,Object,will,collector
来源: https://www.cnblogs.com/miao123-blog/p/16135164.html