public class StrChTest {
String str="good";
char[] ch=new char[]{'n','i','g','h','t'};
public static void main(String[] args) {
StrChTest sc=new StrChTest();
sc.changed(sc.str, sc.ch);
System.out.print(sc.str+"````"+Arrays.toString(sc.ch));
}
public void changed(String stsr,char[] chs)
{
stsr="love";
chs[0]='i';
}
}
String和char[]都是引用类型,那为什么str没被改变,ch数组打印出来被改变了呢?!(和同学争论了会儿,有点领悟,共勉!) |