package basic.java; public class ParametersOfTheMethod {
public static void main(String[] args) {
int a = 10;
int b = 20; System.out.println(a + "===" + b); change(a, b); System.out.println(a + "===" + b);
} public static void change(int a, int b) {
// TODO Auto-generated method stub
System.out.println(a + "===" + b);
a = b;
b = a + b;
System.out.println(a + "===" + b);
} }
如果方法的参数是基本数据类型:形式参数的改变不影响实际参数。
package basic.java; public class ArgsDemo2 {
public static void main(String[] args) {
int arr[] = { 1, 2, 3, 4, 5 }; for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
} change(arr); for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
} public static void change(int[] arr) {
// TODO Auto-generated method stub
for (int i = 0; i < arr.length; i++) {
if (0 == arr[i] % 2) {
arr[i] *= 2;
}
}
}
}
如果参数是引用数据类型:形式参数的改变直接影响实际参数。