7.2 About Method’s arguments(参数)
下面给出值传递 or 地址传递的例子,
public class Test{
/*以下是值传递的例子,结果改不了,*/
public static void mod(int k){
k = k*k;
}
/*以下是地址传递的例子,结果会改变,*/
public static void mod(int[] x){
for(int i=0; i<x.length; i++){
x[i] = x[i]*x[i];
}
}
public static void main(String[] args){
int i = 100;
int[] iArray = {1,2,3};
mod(i);
System.out.println(i);
mod(iArray);
for(int k=0; k<iArray.length; k++)
System.out.println(iArray[k]);
}
}
更多请见:http://www.mark-to-win.com/tutorial/java_1_pass-by-value.html