public class ClassTest{
String str = new String("hello");
char[] ch = {'a','b','c'};
public void fun(String str, char ch[]){
str="world";
ch[0]='d';
}
public static void main(String[] args) {
ClassTest test1 = new ClassTest();
test1.fun(test1.str,test1.ch);
System.out.print(test1.str + " and ");
System.out.print(test1.ch);
}
String对象和数组都在堆中,但是这两个引用类型的变量也就是变量str和ch在栈中,引用类型变量存放的是位于堆中的string对象和数组的地址,也就是说两个引用类型变量分别指向堆中的两个String对象和数组,传递到方法里面的是两个引用类型变量的拷贝,ch数组内容之所以改变了,是因为这相当于引用传递,ch的拷贝在数组内拿到了位于堆中的数组地址去改变了数组的内容,String对象之所以没有改变,是因为它是个常量类,new出来的String对象也是常量,str的拷贝在方法内生成了一个新的String对象在堆中或者常量池中,并指向它,方法外的str还是指向原本的那个string对象。