package cn.hp.demo1;
import java.util.Arrays;
/*
*值传递和引用传递的区别
* 值传递(值是基本数据类型)
* 引用传递(引用是对象)
*/
public class ParamTest {
/*
* 值传递
* */
public static void test1(int n){
n = 2;//方法中改变了n保存的值,猜测和外部的变量n是否有联系?
System.out.println("方法中n = " + n);
}
/*
* 引用传递(传到方法中的对象和原对象仍有联系!!!)
* */
public static void test2(int[]arr){
arr[0]=100;//改变数组对象0下标的元素
System.out.println("方法中arr = " + Arrays.toString(arr));
}
public static void main(String[] args) {
//值传递测试
int n = 1;
test1(n);
System.out.println("外部n = " + n);
/*
* 引用传递测试
* */
int[]arr={1,2};
test2(arr);
System.out.println("外部arr = " +Arrays.toString(arr));
//把对象在堆内存的首地址(位置)保存给不同的变量
int[]brr = {100};
int[]crr = brr;
int[]drr = brr;
crr[0] = 200;
System.out.println("brr[0] = " + brr[0]);
System.out.println("crr[0] = " + crr[0]);
System.out.println("drr[0] = " + drr[0]);
}
}