方法参数传递

对于基本数据类型的参数,形式参数的改变,不影响实际参数的值。

public class Test {
public static void main(String[] args) {
int num = 10;
System.out.println("进方法前:" + num);
change(num);
System.out.println("进方法后:" + num);
}

public static void change(int num) {
num = 20;
}
}

 

 

 

对于引用类型的参数,形式参数的改变,影响实际参数的值

public class Test {
public static void main(String[] args) {
int[] arr = {10, 20, 30};
System.out.println("进方法前:" + arr[1]);
change(arr);
System.out.println("进方法后:" + arr[1]);
}

public static void change(int[] arr) {
arr[1] = 88;
}
}

 

posted @ 2022-01-14 20:59  大灰狼21  阅读(25)  评论(0)    收藏  举报