//值传递 和引用 传递的区别 例子(数组和 对象引用传递 是类似的)
public class Test2 {
int x;
/*private static void chang(int x) {
x=3;
}*/
private static void chang(Test2 obj) {
// obj = new Test2();//这个时候输出5
obj.x=3;
}
private static void chang(int[] x) {
x[0]=3;
}
public static void main(String[] args) {
//一般类型变量作为参数传递 其值不会改变
/* int x = 5;
chang(x);
System.out.println(x);*/
//引用类型变量作为参数传递 值会改变
Test2 obj = new Test2();
obj.x = 5;
chang(obj);
System.out.println(obj.x);
//数组作为参数传递的情况
int x[] = new int[1];
x[0] = 5;
chang(x);
System.out.println(x[0]);
/* Test2[] test2s = new Test2[10];
test2s[0] = new Test2();*/
}
}
输出结果:
3
3