【发布时间】:2010-10-04 03:29:24
【问题描述】:
我有一个关于在 Java 中更改方法中的变量值的问题。
这是我的代码:
public class Test {
public static void funk(int a, int[] b) {
b[0] = b[0] * 2;
a = b[0] + 5;
}
public static void main(String[] args) {
int bird = 10;
int[] tiger = {7};
Test.funk(bird, tiger);
}
}
在Test.funk(bird, tiger)方法执行后,bird的值没有改变——它仍然是10,即使在funk()方法中我们已经用a = b[0] + 5;改变了值
另一方面,数组中元素的值发生变化,因为我们有声明b[0] = b[0] * 2;
我不明白为什么一件事会改变而另一件事不会?谁能帮我解释一下。
【问题讨论】: