Java FAQ:引数で変数渡し

Java FAQ:S012 Q-20

public class C2008051800 {
    static void foo(int x) {
        x = 10;
    }

    public static void main(String[] args) {
        int x = 1;

        foo(x);
        System.out.println(x);
    }
}

で、

1
class C2008051811 {
    int x;
}

public class C2008051810 {
    static void foo(C2008051811 x) {
        x.x = 10;
    }

    public static void main(String[] args) {
        C2008051811 x = new C2008051811();
        x.x = 1;

        foo(x);
        System.out.println(x.x);
    }
}

で、

10

中身を書換えているので、なんか当たり前な気も

class C2008051821 {
    int x;
}

public class C2008051820 {
    static void foo(C2008051821 x) {
        x = new C2008051821();
        x.x = 10;
    }

    public static void main(String[] args) {
        C2008051821 x = new C2008051821();
        x.x = 1;

        foo(x);
        System.out.println(x.x);
    }
}

で、

1