姬長信(Redy)

Java:为什么这种交换方法不起作用?


参见英文答案 > Is Java “pass-by-reference” or “pass-by-value”?????????????????????????????????????82个
我有以下代码:
public class Main {

    static void swap (Integer x, Integer y) {
        Integer t = x;
        x = y;
        y = t;
    }

    public static void main(String[] args) {
       Integer a = 1;
       Integer b = 2;
       swap(a, b);
       System.out.println("a=" + a + " b=" + b);
    } 
}

我希望它打印a = 2 b = 1,但它打印相反.显然交换方法不会交换a和b值.为什么?