首页 > 代码库 > Java参数传递方式

Java参数传递方式

如果方法A调用方法B,如果方法B的参数是基本数据类型,那么方法A向方法B传递参数的值,如果方法B的参数是对象或者数组,那么,方法A向方法B传递参数的引用。

例子如下:

public class VariableTest {

public int membervariable = 0;

public static void main(String[] args){
int param1 = 0;
VariableTest param2 = new VariableTest();
VariableTest param3 = new VariableTest();
int[] param4= {0};
changeParameter(param1, param2, param3, param4);
System.out.println("param1="+param1);
System.out.println("param2.membervariable"+param2.membervariable);
System.out.println("param3.membervariable"+param3.membervariable);
System.out.println("param4[0]="+param4[0]);
}

public static void changeParameter(int param1,VariableTest param2,VariableTest param3,int[] param4){
param1 = 1;
param2.membervariable = 1;
param3 = new VariableTest();
param3.membervariable = 1;
param4[0] = 1;
}


}

打印结果:

param1=0
param2.membervariable1
param3.membervariable0
param4[0]=1