java知识有一个地方不懂,请高手指点,谢谢
public class Test{
public static void add3(Integer i){
int val=i.intValue();
val+=3;
i=new Integer(val);
}
public static void main(String args[]){
Integer i=new Integer(0);
add3(i);
System.out.println(i.intValue());
}
}
请问这个为什么输出的结果是0呢?
[解决办法]
i的值一直都没变
[解决办法]
我觉得是B.
内部类是其外围类的成员,要通过外围类的对象来引用。
[解决办法]
为什么输出0的问题: 应该是值传递问题
[解决办法]
下面那样就对了。。。
public class Lian {
public static void add3(Integer i) {
int val = i.intValue();
val += 3;
i = new Integer(val);
System.out.println(i.intValue());
}
public static void main(String args[]) {
Integer i = new Integer(0);
add3(i);
}
}
main函数中的i为局部变量,所以在main方法中,输出时为此局部变量的值。
[解决办法]