Re: [問題] 關於StringBuffer與區域變數
※ 引述《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
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 4 之 6 篇):