You create the stringbuffer object just to use , why care the init size 這樣不是更好嗎?可以節省內存啊?為什么別人不這么產生對象呢
Object is created and referenced by the variable . at that point the 該方法執行一次,一個新的stringbuffer將被創建并且被sb變量引用。
Object , and now it can be garbage collected . this elimination of the reference is equivalent to nulling the 于是不再有對該stringbuffer對象的引用,現在它就可以被當作垃圾收集了。
Object created in the first line . as long as the method is executing , the reference to the 當該方法執行時,運行時棧保留了一個對stringbuffer對象的引用,這個對象是在程序的第一行產生的。
Is being held onto by the application for much longer than it needs to be - possibly forever if no one ever calls 也就是說,這個死亡的stringbuffer對象被程序保留的時間比它實際需要保留的時間長得多如果再也沒有對該scopingexample方法的調用,它將會永遠保留下去。
Where possible , it is recommended that this class ( stringbuilder ) be used in preference to stringbuffer as it will be faster under most implementations 是否是以不同的觀點、方式或其它專業的效能測試工具做測試。因為依本文所測得的結果,兩項類別所產生的實體在操作上并無明顯的效能差異。
When manipulating strings , use stringbuffer instead of string concatenation due to the immutable nature of string objects , and the need to create an extra string object that eventually must undergo gc 在處理字符串時,使用stringbuffer而不使用字符串連接,因為字符串對象具有不可變的特性,并且需要創建額外的字符串對象,而這些對象最終必須經歷gc 。