值传递和地址传递图解
class Test3 {
public static void main(String[] args) {
int a = 10;
int b = 20;
System.out.println("a:" + a + ",b:" + b);
change(a, b);
System.out.println("a:" + a + ",b:" + b);
//a=10,b=20
//a=10,b=20
//a=20,b=40
//a=10,b=20 值传递
int[] arr = new int[]{ 1, 2, 3, 4, 5 };
change(arr);
System.out.println(arr[1]);//4
System.out.println(arr[3]);//8
}
public static void change(int a, int b) {
System.out.println("a:" + a + ",b:" + b);
a = b;
b = a + b;
System.out.println("a:" + a + ",b:" + b);
}
public static void change(int[] arr) {
//arr=new int[]{1,2,3,4,5};
for (int x = 0; x < arr.length; x++) {
if (arr[x] % 2 == 0) {
arr[x] *= 2;
}
}
}
}