public class Example {
String str=new String("good");
char[] ch={'a','b','c'};
public static void main(String[] args){
Example ex=new Example();
ex.change(ex.str, ex.ch);
System.out.println(ex.str+" and");
System.out.println(ex.ch);
}
public void change(String str,char ch[]){
str="test ok";
ch[0]='g';
}
}
这个题的输出结果为
good and
gbc
为什么str的值没变
而ch[]的值变了呢
答案是Java变量传递的是“句柄”
句柄,就是一个引用,相当于C语言里的指针
java对象指向的是内存中的一个地址
特此更正:java变量传递是值传递,以前理解有错误,特别感谢rrsy23给予斧正