函数的调用-基本数据类型/引用类型
在Java中,传参时,都是以传值的方式进行; 对于基本数据类型,传递的是数据的拷贝;对于引用类型,传递的引用的拷贝;
## file 1 ##
class test{
public static void change (int x,int y){
x = x + y;
y = x - y;
x = x - y;
}
public static void changeArr (int [] num){
num[0 ] = num[0 ] + num[1 ];
num[1 ] = num[0 ] - num[1 ];
num[0 ] = num[0 ] - num[1 ];
}
public static void changeObj (Obj o){
o.x = o.x + o.y;
o.y = o.x - o.y;
o.x = o.x - o.y;
}
public static void main (String[] arg){
int x = 3 ;
int y = 4 ;
change(x,y);
System.out .println("x=" +x+";" +"y=" +y);
int num[] = new int []{3 ,4 };
changeArr(num);
System.out .println("num0=" +num[0 ]+";" +"num1=" +num[1 ]);
Obj pt = new Obj();
pt.x = 3 ;
pt.y = 4 ;
changeObj(pt);
System.out .println("ptx=" +pt.x+";" +"pty=" +pt.y);
}
}
## file 2 ##
class Obj{
int x;
int y;
}