代码:
class AB{
int i=5;
}
class BC{
public void test(AB a){
a.i=10;
}
}
public class ATest{
public static void main(String aa[]){
AB a = new AB();
//Base class variable value
System.out.println(a.i);
BC b = new BC();
//Modifying the object "a"
b.test(a);
//Printing the base class object
System.out.println(a.i);
}
}
// Output : 5
// 10如果是按值传递,则输出应为5和5
发布于 2013-04-07 02:08:00
让我先弄清楚按值传递是什么意思?
这意味着无论你向一个方法传递什么,它都会收到它的副本,而不是实际的地址。
因此,在本例中,您也是在传递变量a的值,并且它的值(引用对象或对对象的地址)被复制到方法(AB)。
https://stackoverflow.com/questions/15854032
复制相似问题