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方法中,输出时为此局部变量的值。
------解决方案--------------------
------解决方案--------------------
可以看看:http://hi.baidu.com/fevagotwh/blog/item/2255bbafedaf7aeffaed5068.html
------解决方案--------------------
我也觉得是3.。。。感觉是引用类型。int类型被包装成Integer是不是也就跟其他的引用类型一样了。如果一样了就是3,如果不是那就是0.LZ我知道的就这么多呀。。。