作者superlubu (劲过吕布)
看板java
标题Re: [问题] 关於StringBuffer与区域变数
时间Thu Jun 15 10:04:19 2006
※ 引述《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