public class PassByValue1
{
int i = 0; 
PassByValue1(int newValue)
{
i = newValue;
}
static void changeInt(int value)
{
value = 98;
System.out.println("value in changeInt() = " + value);
}  static void changeObjRef(PassByValue1 obj)
{
    obj = new PassByValue1(99);
    System.out.println("obj.i in changeObjRef()=" + obj.i);
  }
static void changeObjAttr(PassByValue1 obj)
{
obj.i = 100;
    System.out.println("obj.i in changeObjAttr()=" + obj.i);
  }
  public static void main(String[] args) 
{
    int value=2;
    System.out.println("value before changeInt()=" + value); // value = 2
    changeInt(value); // changeInt() = 98  value 是局部变量,退出方法的时间,value分配的内存收回
    System.out.println("value after changeInt()="+value);  // value = 2 故 value = 2
    PassByValue1 obj = new PassByValue1(2); // 
    System.out.println("------------"); //-------------
    System.out.println("obj before changeObjRef()="+obj); // 地址A
    System.out.println("obj.i before changeObjRef()="+obj.i); // obj.i = 2 
    changeObjRef(obj); // obj.i = 99 obj 在changeObjRef中重新分配了地址,
//要不你可以在方法中尝试一下,System.out.println(obj);你会发现地址变了,
//obj在方法中也是一个局部变量,退出方法的时间,内存收回
    System.out.println("obj.i after changeObjRef()="+obj.i); // obj.i = 2 故obj.i 还是等于2
    System.out.println("obj after changeObjRef()"+obj); //地址A 地址没有变,还是一开始 
//PassByValue1 obj = new PassByValue1(2); 时间的地址
    System.out.println("------------"); // -------- 
    System.out.println("obj before changeObjAttr()="+obj); // 地址A 地址没有变,还是一开始 
//PassByValue1 obj = new PassByValue1(2); 时间的地址
    System.out.println("Obj.i before changeObjAttr()="+obj.i); // obj.i = 2 还是原来的obj.i = 2
    changeObjAttr(obj); // obj.i = 100 obj.i 重新赋值了,
//这个时间也更新了obj 中的i 的值,并记录到obj中,obj没有重新new,是初始的时间的那个
//PassByValue1 obj = new PassByValue1(2);
    System.out.println("obj.i after changeObjAttr()="+obj.i); //obj.i = 100 所以obj.i变成了 100
    System.out.println("obj agter changeObjAttr()="+obj); // 地址A 地址没有变,还是一开始 
//PassByValue1 obj = new PassByValue1(2); 时间的地址,没有重新new  }
}

解决方案 »

  1.   

    以上的回答是在editplus中写的,希望你可以明白,其中的tab字符设为两个字符传值和传reference,我的记法是传值的时间都new了一个object,所以没有影响传入以后,在传出地方的值,它压根就不会变,而传reference的时间,传入的是地址,假如在新方法中,没有重新new object的话,那么所有的改变都是对地址中指向的值的改变,所以传入的时间,对reference的值的改变会反映到reference上。
      

  2.   

    http://expert.csdn.net/Expert/topic/1437/1437310.xml?temp=.699917
    请看上面的贴子里我的回复。