垃圾回收的一个题目:
Given:
3. interface Animal{void makeNoise();}
4. class Horse implements Animal{
5. Long weight = 1200L;
6. public void makeNoise(){System.out.println("whinny");}
7. }
8. public class lcelandic extends Horse{
9. public void makeNoise(){System.out.println("vinny");}
10. public static void main(String[] args) {
11. Icelandic i1 = new lcelandic();
12. Icelandic i2 = new lcelandic();
13. Icelandic i3 = new lcelandic();
14. i3 = i1; i1 = i2; i2 = null; i3 = i1;
15. }
16. }
当程序到达14行时,有多少个符合垃圾回收的条件?
Given:
3. interface Animal{void makeNoise();}
4. class Horse implements Animal{
5. Long weight = 1200L;
6. public void makeNoise(){System.out.println("whinny");}
7. }
8. public class lcelandic extends Horse{
9. public void makeNoise(){System.out.println("vinny");}
10. public static void main(String[] args) {
11. Icelandic i1 = new lcelandic();
12. Icelandic i2 = new lcelandic();
13. Icelandic i3 = new lcelandic();
14. i3 = i1; i1 = i2; i2 = null; i3 = i1;
15. }
16. }
当程序到达14行时,有多少个符合垃圾回收的条件?
i1=i2不回收
i2=null不回收
i3=i1;回收一个所以总共回收了2个,个人看法
因为new了3个对象,i3 = i1; i1 = i2; i2 = null; i3 = i1;
最后只有i3没有被指向,所以它是垃圾对象。
Given: 3. interface Animal{void makeNoise();}
4. class Horse implements Animal{
5. Long weight = 1200L;
6. public void makeNoise(){System.out.println("whinny");}
7. }
8. public class lcelandic extends Horse{
9. public void makeNoise(){System.out.println("vinny");}
10. public static void main(String[] args) {
11. Icelandic i1 = new lcelandic(); //大家重点考虑一下这个对象回收了没有
12. Icelandic i2 = new lcelandic();
13. Icelandic i3 = new lcelandic();
14. i3 = i1; i1 = i2; i2 = null; i3 = i1;
15. }
16. } 当程序到达14行时,有多少个符合垃圾回收的条件? 我将代码编辑了一下,大家好看。