本帖最后由 王红霞 于 2012-7-12 07:33 编辑
为什么这两个程序仅因为彩色部分不同 结果不同呢?
public class Demo3 {
int a;
int b;
public void print_3() {
a = 0;
b = 0;
int c = 0;
g(b, c);
System.out.println(a + ", " + b + ", " + c);
}
public void g(int b, int c) {
a = 1;
b = 2;
c = 3;
}
public static void main(String args[]) {
Demo3 d = new Demo3();
d.print_3();
}
}
输出结果是:1, 0, 0
而
public class Demo4 {
int a;
int b;
public void print_4() {
a = 0;
b = 0;
int[] c = {0};
g(b, c);
System.out.println(a + ", " + b + ", " + c[0]);
}
public void g(int b, int[] c) {
a = 1;
b = 2;
c[0] = 3;
}
public static void main(String args[]) {
Demo4 d = new Demo4();
d.print_4();
}
}
输出结果是:1, 0, 3
|