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.print(ex.str+" and "); for(int i=0;i<ex.ch.length;i++){System.out.print(ex.ch[i]);} } public void change(String str,char ch[]){ str="test ok"; ch[0]='g'; } }A good and abc B good and gbc C test ok and abc D test ok and gbc
4 回答

翻翻過去那場雪
TA貢獻(xiàn)2065條經(jīng)驗 獲得超14個贊
樓上說的不完全對,就算在change方法中不傳str參數(shù),change方法依舊無法改變str的值。
說到底,str= "abc"與str=new String("abc")在內(nèi)存中是完全不同的表現(xiàn)形式。
前者直接指向字符串,而后者指向的是一個String對象。
添加回答
舉報
0/150
提交
取消