java 函数参数传递都是传值
对于基本数据类型,传递的是数据的拷贝;
对于引用类型,传递的是引用的拷贝;
int x = 3;
int y = 4;
change(x, y);
System.out.println("x=" + x + "y=" + y);
//不可交换
int[] num1 = new int[]{3,4};
change(num1);
System.out.println("num x=" + num1[0] + "num y=" + num1[1]);
//可交换
Point pt = new Point();
pt.x = 3;
pt.y = 4;
change(pt);
System.out.println("pt.x=" + pt.x + "pt.y=" + pt.y);
//可交换
public static void change(int x, int y) {
x = x + y;
y = x - y;
x = x - y;
}
public static void change(int[] num) {
num[0] = num[0] + num[1];
num[1] = num[0] - num[1];
num[0] = num[0] - num[1];
}
public static void change(Point pt) {
pt.x = pt.x + pt.y;
pt.y = pt.x - pt.y;
pt.x = pt.x - pt.y;
}
posted on 2011-01-04 21:14
冯占科 阅读(176)
评论(0) 编辑 收藏