目录
定义:
StringBuffer类被 final 所修饰,不能被继承。
StringBuffer类实现了2个接口:
①:Serializable 序列化接口,表示对象可以被序列化。
②:CharSequence 字符序列接口,提供了几个对字符序列进行只读访问的方法,比如:length()、charAt()、subSequence()、toString()方法等。
主要变量:
private transient char[] toStringCache; //用来缓存toString()方法返回的最近一次的value数组中的字符。当修改StringBuffer对象时会被清除
// AbstractStringBuilder.java
char[] value; //用来存储字符序列中的字符。value是一个动态的数组,当存储容量不足时,会对它进行扩容
int count; //表示value数组中已存储的字符数
构造方法:
public StringBuffer() {
super(16);
}
public StringBuffer(int capacity) {
super(capacity);
}
public StringBuffer(String str) {
super(str.length() + 16);
append(str);
}
public StringBuffer(CharSequence seq) {
this(seq.length() + 16);
append(seq);
}
// AbstractStringBuilder.java
AbstractStringBuilder(int capacity) {
value = new char[capacity];
}
StringBuffer类提供了4个构造方法。构造方法主要完成了对value数组的初始化。其中:
默认构造方法设置了value数组的初始容量为16。
第2个构造方法设置了value数组的初始容量为指定的大小。
第3个构造方法接受一个String对象作为参数,设置了value数组的初始容量为String对象的长+16,并把String对象中的字符添加到value数组中。
第4个构造方法接受一个CharSequence对象作为参数,设置了value数组的初始容CharSequence对象的长度+16,并把CharSequence对象中的字符添加到value数组中。
StringBuffer的append方法
StringBuffer sb = new StringBuffer("abc");//原来数据的长度:3个字符
sb.append("defghijklmnopqrst");//新增加数据的长度:17个字符
//str-->"defghijklmnopqrst"
public AbstractStringBuilder append(String str) {
//新增加的数据为null则直接返回字符串"null"
if (str == null) str = "null";
int len = str.length();//新增加数据的长度为:17
ensureCapacityInternal(count + len);//扩容:count+len=3 + 17
str.getChars(0, len, value, count);//将原来的数据复制到扩容后的数组中
count += len;
return this;
}
//int minimumCapacity = 20
private void ensureCapacityInternal(int minimumCapacity) {
// overflow-conscious code:判断是否需要扩容,也就是说原来的capacity是否足够大
if (minimumCapacity - value.length > 0) //20-19=1,1>0
expandCapacity(minimumCapacity);
}
void expandCapacity(int minimumCapacity) { //int minimumCapacity=20
int newCapacity = value.length * 2 + 2; //新的容量capacity=原来的长度*2+2
//扩容后的容量-字符串实际长度<0(就是说如果扩容后还装不下),
//则使用字符串实际长度作为StringBuffer的capacity
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);
}
//copyOf方法用于创建一个新数组,新数组的长度是扩容后的长度,并将原来的值复制到新的数组中
//这里需要注意,虽然数组是新的,但是StringBuffer还是原来的StringBuffer
//original:StringBuffer中原来的值,也就是'abc'
//newLength:新的长度,19*2+2=40
public static char[] copyOf(char[] original, int newLength) {
char[] copy = new char[newLength];
System.arraycopy(original, 0, copy, 0,
Math.min(original.length, newLength));
return copy;
}
//int srcBegin:字符串str中要复制的第一个字符的索引
//int srcEnd:字符串str中要复制的最后一个字符之后的索引
//char dst[]:目标数组
//int dstBegin:目标数组中的起始偏移量
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
if (srcBegin < 0) {
throw new StringIndexOutOfBoundsException(srcBegin);
}
if (srcEnd > value.length) {
throw new StringIndexOutOfBoundsException(srcEnd);
}
if (srcBegin > srcEnd) {
throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
}
System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
StringBuffer是用char[] value在存储数据。如果用new StringBuffer() 新建,默认给该数组分配16char的空间。对StringBuffer进行append时会先用ensureCapacityInternal判断是否有足够的空间,如果空间不够,就尝试将容量扩大到原来的2倍(+2),判断够不够;如果够,就以新的容量新建一个数组;如果不够,就以需要的容量新建数组。然后把原数组里的数据复制到新数组中。