方法参数的传递机制 ---- 值传递

一、方法的参数传递机制

1. 形参是基本数据类型
- 传递数据值
2. 实参是引用数据类型
- 传递地址值
- 特殊的类型:String、包装类等对象不可变性
import java.util.Arrays;

public class Dome03_04 {

    public static void main(String[] args) {
        int i = 1;
        String str = "hello";
        Integer num = 200;
        int[] arr = {1,2,3,4,5};
        MyData my = new MyData();

        change(i,str,num,arr,my);
        System.out.println("i = "+ i);
        System.out.println("str = " + str);
        System.out.println("num = " + num);
        System.out.println("num = " + Arrays.toString(arr));
        System.out.println("my.a = " + my.a);
    }

    public static void change(int j,String s,Integer n,int[] a,MyData m){
        j += 1;
        s += "world";
        n += 1;
        a[0] += 1;
        m.a += 1;
    }

}
class MyData{
    int a = 10;
}


"D:Program FilesJavajdk1.8.0_231injava.exe" 
i = 1
str = hello
num = 200
num = [2, 2, 3, 4, 5]
my.a = 11

Process finished with exit code 0

相关链接:为什么说Java中只有值传递 [https://blog.csdn.net/bjweimengshu/article/details/79799485]

原文地址:https://www.cnblogs.com/shaoyu/p/12407896.html