1. 一共创建了5个对象, 最后一个对象被o reference,  所以42。3。 一共创建了5个对象, 但是变量o是在循环内声明的,出了循环变量就不起作用了, 所以5个对象都要被回收。

解决方案 »

  1.   

    Object o ;  
    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谁解释一下
      

  2.   

    那么第二个呢?为什么是5?还有第一个题目,当一个对象为null时,给不给这个对象分配内存?在java里是不是只有new 的时候才给分配内存?
      

  3.   

    六熊熊我也不知道不过你点给我点分数啊 我是mattrew希望你的问题能早点解决
      

  4.   

    第二个你试试就知道了,编译不通过,因为o作为本地变量没有初始化。
    对象没有是null的时候,指向对象的引用才有null的时候。
    new给对象分配内存。
      

  5.   

    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");
     } 
    }
    }
    这个小程序编译可以过去,这样的话,第二个问题就应该有答案。
      

  6.   

    从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); 
     }
    }
      

  7.   

    抱歉,是这样的,在第二问题中如果在for(){}后面使用o的话编译就会出错。所以在一个正确编译的程序中,o不可能在for循环外被使用,所以在退出for循环时o可以被垃圾回收。
      

  8.   

    垃圾回收器只回收那些由NEW分配的内存。
    而上面3个o都是由5个NEW分配的,所以,程序结束是都要回收,即都是5个对象被回收。
    为什么第一个会是4呢?麻烦能解释一下吗?
      

  9.   

    how do you know the second is 5?
      

  10.   

    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.
      

  11.   

    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.
      

  12.   

    ajoo:呵呵,说的不错。
    firefly2000:第一题中o在开始时做了初始化,所以可以在for循环结束后继续被使用,既然它可能被继续使用它所指向的对象就不能被垃圾收集。而后两个题中所有对象出了for循环就不会被使用了,所以全部可以被垃圾收集。
      

  13.   

    后两个不会被使用就是因为没有做初始化?那么我要是在第三个题中做了初始化,即:For(int  i=0;i<5;i++)  {    
           Object  o  = null;
       }   
    这样应该有几个被回收呢?
      

  14.   

    o gets out of scope after the loop ah!
      

  15.   

    第二个是因为没有初始化,第三个o在for循环内定义,外面看不到。