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 }
}
{
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 }
}
请看上面的贴子里我的回复。