对JAVA中值传递和引用传递的理解
一、值传递
对于基本数据类型的参数,形式参数的改变,不影响实际参数的改变!
package com.itheima_04;
public class ArgsDemo01 {
public static void main(String[] args) {
int n=100;
System.out.println("调用change方法前:"+n);
change(n);
System.out.println("调用change方法后:"+n);
}
public static void change(int n){
n = 200;
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a5c7b9ec739463fdfcf5bbd628c72694.png)
二、引用传递
对于引用类型的参数,形式参数的改变,影响实际参数的值!
package com.itheima_04;
public class ArgsDemo02 {
public static void main(String[] args) {
int[] arr = {10,20,30};
System.out.println("调用change方法前:"+arr[1]);
change(arr);
System.out.println("调用change方法后:"+arr[1]);
}
public static void change(int[] arr){
arr[1] = 100;
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/ae7ed4a1dccd291677d7bb0375faea7e.png)