public class Test {
Integer integer = new Integer(2);
char[]ch={'a','b','c'};
public static void main(String[] agrs) {
Test ex=new Test();
ex.change(ex.integer,ex.ch);
System.out.print(ex.integer+" and ");
System.out.print(ex.ch);
}
public void change(Integer str,char ch[]){
str = 1;ch[0]='g';
System.out.println(str);
}
}1
2 and gbc代码跟运行结果如上,问为什么Integer不会改变 char会改变?
1 回答

叮当猫咪
TA贡献1776条经验 获得超12个赞
Interger你传入的是一个对象,在函数调用的时候这个会自动在程序堆栈中生成这个对象的副本,在函数里改变的是那个在堆栈中的副本,而char你传入的是一个指针,在函数中你是直接对指针指向的位置的进行赋值,因此该位置的值就被改变了
添加回答
举报
0/150
提交
取消