Re: [問題] 關於StringBuffer與區域變數

看板java作者 (隱者)時間19年前 (2006/06/15 10:14), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串4/6 (看更多)
※ 引述《superlubu (勁過呂布)》之銘言: : ※ 引述《qrtt1 (隱者)》之銘言: : : import java.io.IOException; : : import java.util.Arrays; : : import java.util.List; : : public class Alias { : : public static void main(String[] args) throws IOException { : : StringBuffer a = new StringBuffer("A"); : : StringBuffer b = new StringBuffer("B"); : : // in function, the args is an alias for params : : // make alias for b : : StringBuffer y = b; : : // y's content equals to b's content : : System.out.println(y.toString()); : : // a.append method create a new StringBuffer : : // y is assigned to new instance : : y=a.append(b); : Returns: : a reference to this StringBuffer object. : StringBuffer.append() 不會 create 一個新的 StringBuffer object, 而是 : 回傳本來的那個 StringBuffer object. : 只要用 System.out.println( (y==a) ); 就可驗證了。 orz..不好意思, 俺講錯了. 但重點是reference y和b的關係就此中止 並且y變和了a System.out.println(y.equals(a)); : : System.out.println(y.toString()); : : // now b is not associated with y : : System.out.println(b.toString()); : : System.out.println(a.toString()); : : } : : } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 163.26.34.214
文章代碼(AID): #14aCAAne (java)
討論串 (同標題文章)
文章代碼(AID): #14aCAAne (java)