class Test_1 { public static void main(String[] args) { Object o; for(int i=0;i<5;i++) { o = new Object(); System.out.println("aaaa"); } } } 这个小程序编译可以过去,这样的话,第二个问题就应该有答案。
从c++的观点应该是445,因为Object o = null; 和Object o;一样,java会自动将未初始化的句柄(栈上)设为null,而且二者的scope一样。 但是我试过了,答案确实是455。 不初始化的话,句柄也应该是null,但是如果初始化null的话,最后那个new的会保留,不初始化则无效class Test_1 { private int i; /* public Test_1(int x) { //System.out.println("ctor"); i = x; } */ public Test_1() { //System.out.println("ctor"); i = -99; } public static void main(String[] args) { /* Test_1 o = new Test_1() ; System.out.println("o is: " + o); System.out.println("i is: " + o.i); System.out.println("start for"); for(int i=0;i<5;i++) { o = new Test_1(); System.out.println("o is: " + o); System.out.println("i is: " + o.i); } System.out.println("end of for"); System.out.println("o is: " + o); System.out.println("i is: " + o.i);*/ /* Test_1 o = null ; System.out.println("o is: " + o); // System.out.println("i is: " + o.i); System.out.println("start for"); for(int i=0;i<5;i++) { o = new Test_1(); System.out.println("o is: " + o); System.out.println("i is: " + o.i); } System.out.println("end of for"); System.out.println("o is: " + o);//有效 System.out.println("i is: " + o.i); */
Test_1 o ; // System.out.println("o is: " + o); // System.out.println("i is: " + o.i); System.out.println("start for"); for(int i=0;i<5;i++) { o = new Test_1(); System.out.println("o is: " + o); System.out.println("i is: " + o.i); } System.out.println("end of for"); // System.out.println("o is: " + o); //无效 // System.out.println("i is: " + o.i); } }
ok. I guess Java is acting as a big brother here. if you don't initialize the o in the same scope, it does not allow you to reference o syntactically.And since it does not allow you to reference it, the garbage collector is free to collect the object that o refers to.
actually, even for step 1, if you don't reference o after the loop, the optimizer may choose to collect all 5 after the loop.The point is: compiler may do some scope analysis, and if it can deduce that an object will not be used any more, it is free to collect it.
for(int i=0;i<5;i++) o = new Object(); Method Test()
0 aload_0
1 invokespecial #1 <Method java.lang.Object()>
4 returnMethod void main(java.lang.String[])
0 iconst_0
1 istore_2
2 goto 16
5 new #2 <Class java.lang.Object>
8 dup
9 invokespecial #1 <Method java.lang.Object()>
12 astore_1
13 iinc 2 1
16 iload_2
17 iconst_5
18 if_icmplt 5
21 return Object o = null;
for(int i=0;i<5;i++) o = new Object(); Method Test()
0 aload_0
1 invokespecial #1 <Method java.lang.Object()>
4 returnMethod void main(java.lang.String[])
0 aconst_null
1 astore_1
2 iconst_0
3 istore_2
4 goto 18
7 new #2 <Class java.lang.Object>
10 dup
11 invokespecial #1 <Method java.lang.Object()>
14 astore_1
15 iinc 2 1
18 iload_2
19 iconst_5
20 if_icmplt 7
23 return谁解释一下
对象没有是null的时候,指向对象的引用才有null的时候。
new给对象分配内存。
{
public static void main(String[] args)
{
Object o;
for(int i=0;i<5;i++)
{
o = new Object();
System.out.println("aaaa");
}
}
}
这个小程序编译可以过去,这样的话,第二个问题就应该有答案。
但是我试过了,答案确实是455。
不初始化的话,句柄也应该是null,但是如果初始化null的话,最后那个new的会保留,不初始化则无效class Test_1
{
private int i;
/* public Test_1(int x)
{
//System.out.println("ctor");
i = x; }
*/
public Test_1()
{
//System.out.println("ctor");
i = -99;
} public static void main(String[] args)
{
/* Test_1 o = new Test_1() ;
System.out.println("o is: " + o);
System.out.println("i is: " + o.i);
System.out.println("start for");
for(int i=0;i<5;i++)
{
o = new Test_1();
System.out.println("o is: " + o);
System.out.println("i is: " + o.i);
}
System.out.println("end of for");
System.out.println("o is: " + o);
System.out.println("i is: " + o.i);*/
/* Test_1 o = null ;
System.out.println("o is: " + o);
// System.out.println("i is: " + o.i);
System.out.println("start for");
for(int i=0;i<5;i++)
{
o = new Test_1();
System.out.println("o is: " + o);
System.out.println("i is: " + o.i);
}
System.out.println("end of for");
System.out.println("o is: " + o);//有效
System.out.println("i is: " + o.i); */
Test_1 o ;
// System.out.println("o is: " + o);
// System.out.println("i is: " + o.i);
System.out.println("start for");
for(int i=0;i<5;i++)
{
o = new Test_1();
System.out.println("o is: " + o);
System.out.println("i is: " + o.i);
}
System.out.println("end of for");
// System.out.println("o is: " + o); //无效
// System.out.println("i is: " + o.i);
}
}
而上面3个o都是由5个NEW分配的,所以,程序结束是都要回收,即都是5个对象被回收。
为什么第一个会是4呢?麻烦能解释一下吗?
if you don't initialize the o in the same scope,
it does not allow you to reference o syntactically.And since it does not allow you to reference it, the garbage collector is free to collect the object that o refers to.
firefly2000:第一题中o在开始时做了初始化,所以可以在for循环结束后继续被使用,既然它可能被继续使用它所指向的对象就不能被垃圾收集。而后两个题中所有对象出了for循环就不会被使用了,所以全部可以被垃圾收集。
Object o = null;
}
这样应该有几个被回收呢?