参数的传递

例1:

    public static void main(String[] args) {
        int a = 2;
        int b = 4;
        change(2, 4);
        System.out.println(a);//猜猜输出a的值是多少?
        System.out.println(b);//猜猜输出b的值是多少?
    }

    public static void change(int a, int b) {
        a = a + b;
        b = a + b;
    }

解释:a、b的作用域仅限于main方法,和change()方法的a、b不是同一个,调用change()函数只是把数值传给了参数a、b,上述写法很容易造成误解;

如果这样写就容易理解了:

 public static void main(String[] args) {
        int a = 2;
        int b = 4;
        change(2, 4);
        System.out.println(a);
        System.out.println(b);
    }

    public static void change(int x, int y) {
        x = x + y;
        y = x + y;
    }
原文地址:https://www.cnblogs.com/a591378955/p/7847561.html