Java 中引用类型变量做参数传递时 所涉及的问题:
地址传递问题(区别于 值传递)
class test{
public int a = 10;
}
public class TestValObject {
public static void another(test v1,int i) {
i=0;
v1.a=40; // 局部变量v1存储 test变量的地址,(10)->(40)
test v0 = new test();
v1 = v0; // v1 指向改变
System.out.println(v1.a);
System.out.println(i);
}
public static void main(String[] args) {
test v1 = new test();
int i=20;
another(v1, i); // 传递地址
System.out.println(v1.a);
System.out.println(i);
}
}
// 10 0 40 20
(默默的加一句,我被坑的好惨呀!)