Re: [問題] 關於StringBuffer與區域變數
※ 引述《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) ); 就可驗證了。
: System.out.println(y.toString());
: // now b is not associated with y
: System.out.println(b.toString());
: System.out.println(a.toString());
: }
: }
--
勁過呂布的勁過相薄...
http://www.wretch.cc/album/superlubu
亂七八糟的,不好意思 m(_ _)m
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 147.8.130.225
討論串 (同標題文章)