StringBuffer類表示一個可變的字符序列。StringBuffer的API與StringBuilder互相兼容,但是StringBuffer是線程安全的。在可能的情況下,建議優先使用StringBuilder,因為在大多數實現中它比StringBuffer更快。
一、類定義
public final class StringBufferextends AbstractStringBuilderimplements java.io.Serializable, CharSequence
{...
}
StringBuffer類被 final 所修飾,因此不能被繼承。
StringBuffer類繼承于 AbstractStringBuilder類。實際上,AbstractStringBuilder類具體實現了可變字符序列的一系列操作,比如:append()、insert()、delete()、replace()、charAt()方法等。值得一提的是,StringBuilder也是繼承于AbstractStringBuilder類。
StringBuffer類實現了2個接口:
- Serializable 序列化接口,表示對象可以被序列化。
- CharSequence 字符序列接口,提供了幾個對字符序列進行只讀訪問的方法,比如:length()、charAt()、subSequence()、toString()方法等。
二、成員變量
private transient char[] toStringCache;// AbstractStringBuilder.javachar[] value;int count;
-
value、count這兩個變量是繼承自父類
-
toStringCache 用來緩存toString()方法返回的最近一次的value數組中的字符。當修改StringBuffer對象時會被清除。
三、構造方法
//默認構造方法設置了value數組的初始容量為16。
public StringBuffer() {super(16);
}
//設置了value數組的初始容量為指定的大小。
public StringBuffer(int capacity) {super(capacity);
}
//接受一個String對象作為參數,設置了value數組的初始容量為String對象的長度+16,并把String對象中的字符添加到value數組中。
public StringBuffer(String str) {super(str.length() + 16);append(str);
}
//接受一個CharSequence對象作為參數,設置了value數組的初始容量為CharSequence對象的長度+16,并把CharSequence對象中的字符添加到value數組中。
public StringBuffer(CharSequence seq) {this(seq.length() + 16);append(seq);
}// AbstractStringBuilder.java
AbstractStringBuilder(int capacity) {value = new char[capacity];
}
StringBuffer類提供了4個構造方法。構造方法主要完成了對value數組的初始化。
四、普通方法
StringBuilder實現了AbstractStringBuilder和CharSequence,他的方法都來自于這兩個類,絕大部分都是通過super來調用的。
4.1、append()方法
@Override
public synchronized StringBuffer append(boolean b) {toStringCache = null;super.append(b);return this;
}// AbstractStringBuilder.java
public AbstractStringBuilder append(boolean b) {if (b) {ensureCapacityInternal(count + 4);value[count++] = 't';value[count++] = 'r';value[count++] = 'u';value[count++] = 'e';} else {ensureCapacityInternal(count + 5);value[count++] = 'f';value[count++] = 'a';value[count++] = 'l';value[count++] = 's';value[count++] = 'e';}return this;
} @Override
public synchronized StringBuffer append(String str) {toStringCache = null;super.append(str);return this;
}// AbstractStringBuilder.java
public AbstractStringBuilder append(String str) {if (str == null)return appendNull();int len = str.length();ensureCapacityInternal(count + len);str.getChars(0, len, value, count);count += len;return this;
}
調用了父類AbstractStringBuilder類中對應的方法。最后,append()方法返回了StringBuffer對象自身。
append()方法將指定參數類型的字符串表示形式追加到字符序列的末尾。它可以接受boolean、char、char[]、CharSequence、double、float、int、long、Object、String、StringBuffer這些類型的參數。這些方法最終都,以便用戶可以鏈式調用StringBuilder類中的方法。
AbstractStringBuilder類的各個append()方法大同小異。append()方法在追加字符到value數組中之前都會調用ensureCapacityInternal()方法來確保value數組有足夠的容量,然后才把字符追加到value數組中。
4.2、toString()方法
@Override
public String toString() {// Create a copy, don't share the arrayreturn new String(value, 0, count);
}
4.3、writeObject和readObject
/*** Save the state of the {@code StringBuilder} instance to a stream* (that is, serialize it).** @serialData the number of characters currently stored in the string* builder ({@code int}), followed by the characters in the* string builder ({@code char[]}). The length of the* {@code char} array may be greater than the number of* characters currently stored in the string builder, in which* case extra characters are ignored.*/private void writeObject(java.io.ObjectOutputStream s)throws java.io.IOException {s.defaultWriteObject();s.writeInt(count);s.writeObject(value);}/*** readObject is called to restore the state of the StringBuffer from* a stream.*/private void readObject(java.io.ObjectInputStream s)throws java.io.IOException, ClassNotFoundException {s.defaultReadObject();count = s.readInt();value = (char[]) s.readObject();}
實現自Serializable接口后,可定制的序列化過程
4.4、toStringCache()
StringBuffer中有一個toStringCache 就像它的名字一樣,toString()方法的cache
簡言之就是緩存toString方法的,每次調用toString會檢查這個字段,如果不為null將會使用它進行對象創建
如果為null 將會給他初始化賦值,也就是緩存,當調用其他的任何方法改變StringBuffer時,就會把toStringCache進行清空
五、總結
StringBuffer類將所有操作字符序列的方法都添加了 synchronized 關鍵字來修飾,因此,StringBuffer類是線程安全的。
-
他是可變的字符序列的抽象模型,定義了可變字符序列的公共行為
-
它是一個抽象類,針對一些操作提供了默認的實現