方法参数传递过程分析

输出如下代码的运行结果

public class MethodParamsPassTest {
    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("arr = " + 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;
}

输入结果

i = 1
str = hello
num = 200
arr = [2, 2, 3, 4, 5]
my.a = 11

考点

  • 方法的参数传递机制
    • 形参是基本数据类型
      • 传递数据值
  • 实参是引用数据类型
    • 传递地址值
    • 特殊的类型:String、包装类等对象不可变性

内存分析图

方法的参数传递机制

源码地址

源码地址

最近的文章

递归与迭代分析

编程题:有n步台阶,一次只能上1步或2步,共有多少种走法?递归循环迭代核心代码递归TestStep.java public class TestStep { /** * 实现f(n):求n步台阶,一共有几种走法 * * @param n * @return */ publi…

继续阅读
更早的文章

类的初始化和实例化过程

如下代码,运行Son类主方法运行结果是什么?Father.javapublic class Father { private int i = test(); private static int j = method(); static { System.out.pr…

继续阅读