值传递或值拷贝
如 n1 = 10; n2= n1;就是把n1的值拷贝一份赋给n2 此时改变n2的值不会影响到n1
n1 = 10;
n2= n1;
n2= 20;
n1还是等于10
引用传递
int arr[] = {1,2,3};
int arr2 = arr;
引用传递就是把arr的地址复制一份给arr2 arr2也可以去更改arr的值
如 arr2[0] = 10; arr[] ={10,2,3}
数组的拷贝
创建一个新的数组arr2 开辟一个跟arr一样大小的空间 此时新的数组arr值是空的 遍历arr 把arr的值赋给arr2
int arr[] = {1,2,3};
int arr2[] = new int [arr.length];
-----用 for循环遍历arr 把每一个值赋给arr2
arr2[i] = arr[i]
public class Array01{
public static void main(String[] args) {
int arr[] = {1,2,3};
int arr2[] = new int [arr.length];
for (int i = 0 ;i<arr.length ;i++) {
arr2[i] = arr[i];
}
for (int a = 0 ;a<arr2.length ;a++ ) {
System.out.print(arr[a]);
}
}
}