JDK源碼閱讀--StringBuilder

 

 

public final class StringBuilder
extends AbstractStringBuilder
implements java.io.Serializable, CharSequence


被final修飾,表示StringBuilder不能被其它類繼承。
繼承了AbstractStringBuilder抽象類,實現了 Serializable、CharSequence接口。
StringBuilder中的方法沒有被Synchronized修飾,因此StringBuilder不是線程安全的。

 1  /**
 2      * Constructs a string builder with no characters in it and an
 3      * initial capacity of 16 characters.
 4      */
 5     public StringBuilder() {
 6         super(16);
 7     }
 8 
 9     /**
10      * Constructs a string builder with no characters in it and an
11      * initial capacity specified by the {@code capacity} argument.
12      *
13      * @param      capacity  the initial capacity.
14      * @throws     NegativeArraySizeException  if the {@code capacity}
15      *               argument is less than {@code 0}.
16      */
17     public StringBuilder(int capacity) {
18         super(capacity);
19     }
20 
21     /**
22      * Constructs a string builder initialized to the contents of the
23      * specified string. The initial capacity of the string builder is
24      * {@code 16} plus the length of the string argument.
25      *
26      * @param   str   the initial contents of the buffer.
27      */
28     public StringBuilder(String str) {
29         super(str.length() + 16);
30         append(str);
31     }

StringBuilder的容量默認是16,當容量不夠的時候,容量會繼續增長16,依次類推。java

相關文章
相關標籤/搜索