標籤:size string類 void highlight array width png cap content
public final class StringBuffer extends AbstractStringBuilder implements java.io.Serializable, CharSequence { /** * Constructs a string buffer with no characters in it and an * initial capacity of 16 characters. */ public StringBuffer() { super(16); } /** * Constructs a string buffer with no characters in it and * the specified initial capacity. */public StringBuffer(int capacity) { super(capacity); }/** * Constructs a string buffer initialized to the contents of the * specified string. The initial capacity of the string buffer is * <code>16</code> plus the length of the string argument. * * @param str the initial contents of the buffer. * @exception NullPointerException if <code>str</code> is <code>null</code> */public StringBuffer(String str) { super(str.length() + 16); append(str); }
StringBuffer 是一個安全執行緒的可變的字元序列。它繼承於AbstractStringBuilder,實現了CharSequence介面。
StringBuilder 也是繼承於AbstractStringBuilder的子類;但是,StringBuilder和StringBuffer不同,前者是非安全執行緒的,後者是安全執行緒的。
StringBuffer的內部實現方式和String不同,StringBuffer在進行字串處理時,不產生新的對象,在記憶體使用量上要優於String類。
所以在實際使用時,如果經常需要對一個字串進行修改,例如插入、刪除等操作,使用StringBuffer要更加適合一些。
在StringBuffer類中存在很多和String類一樣的方法,這些方法在功能上和String類中的功能是完全一樣的。
但是有一個最顯著的區別在於,對於StringBuffer對象的每次修改都會改變對象自身,這點是和String類最大的區別。
對於StringBuffer的append操作基本是在AbstractStringBuilder繼承實現,看源碼:
abstract class AbstractStringBuilder implements Appendable, CharSequence { char[] value; //The value is used for character storage. int count; //The count is the number of characters used.//Appends the specified string to this character sequence.public AbstractStringBuilder append(String str) { if (str == null) str = "null"; int len = str.length(); ensureCapacityInternal(count + len); str.getChars(0, len, value, count); count += len; return this; } // Documentation in subclasses because of synchro difference public AbstractStringBuilder append(StringBuffer sb) { if (sb == null) return append("null"); int len = sb.length(); ensureCapacityInternal(count + len); sb.getChars(0, len, value, count); count += len; return this; } // Documentation in subclasses because of synchro difference public AbstractStringBuilder append(CharSequence s) { if (s == null) s = "null"; if (s instanceof String) return this.append((String)s); if (s instanceof StringBuffer) return this.append((StringBuffer)s); return this.append(s, 0, s.length()); } /** * Appends the string representation of the {@code char} array * argument to this sequence. */public AbstractStringBuilder append(char[] str) { int len = str.length; ensureCapacityInternal(count + len); System.arraycopy(str, 0, value, count, len); count += len; return this; }
}/***************其他追加字元方法省略介紹了***********/
添加字元時的擴容方法介紹:
/** * This implements the expansion semantics of ensureCapacity with no * size check or synchronization. */ void expandCapacity(int minimumCapacity) { int newCapacity = value.length * 2 + 2; if (newCapacity - minimumCapacity < 0) newCapacity = minimumCapacity; if (newCapacity < 0) { if (minimumCapacity < 0) // overflow throw new OutOfMemoryError(); newCapacity = Integer.MAX_VALUE; } value = Arrays.copyOf(value, newCapacity); }
每次添加字元時擴容為:int newCapacity = value.length * 2 + 2;
java.lang(StringBuffer)