public class StringTest1 {
String str = new String("good");
char[] ch = {'t','e','s','t'};
public void change(String str ,char ch[]) {
str = "test ok" ;
ch[0] = 'b';
}
public static void main(String[] args) {
// TODO Auto-generated method stub
StringTest1 st = new StringTest1();
st.change(st.str,st.ch);
System.out.println(st.str);
System.out.println(st.ch);
}
}
输出结果:
原因:
传入的st.str是st对象的一个属性,所以相当于是值传递。
public class StringTest1 {
String str = new String("good");
char[] ch = {'t','e','s','t'};
public void change(String str ,char ch[]) {
this.str = "test ok" ;
ch[0] = 'b';
}
public static void main(String[] args) {
// TODO Auto-generated method stub
StringTest1 st = new StringTest1();
st.change(st.str,st.ch);
System.out.println(st.str);
System.out.println(st.ch);
}
}