对象的生命周期的是随着引用的,引用这个对象的引用消失这个对象随后也就会被
垃圾回收器(gc)所回收,
三种情况
public class dog {
public static void main(String[] args) {
}
void test() {
dog d = new dog();
}
}
这种情况下随着方法的执行完毕对象就会被回收
public class dog {
public static void main(String[] args) {
}
void test() {
dog d = new dog();
dog d2 = new dog();
d2=d; //这个时候 随着对象引用赋值给其他变量,引用消失对象也会随之销毁
}
}
public class dog {
public static void main(String[] args) {
}
void test() {
dog d = new dog();
d = null;//这个时候引用已经是空的了,没有指向任何对象 对象也会被销毁
}
}