JDK 源码:AbstractStringBuilder
类详解
AbstractStringBuilder
是 StringBuilder
和 StringBuffer
的抽象基类,它为可变字符串提供了通用的实现。通过分析 AbstractStringBuilder
的源码,我们可以更好地理解 StringBuilder
和 StringBuffer
的工作原理。
1. 基本属性
abstract class AbstractStringBuilder implements Appendable, CharSequence {
char[] value; // 存储字符的数组
int count; // 当前字符数
}
value
:字符数组,用于存储字符串的内容。count
:当前字符串的长度。
2. 构造方法
AbstractStringBuilder
提供了几个构造方法来初始化字符数组:
AbstractStringBuilder(int capacity) {
value = new char[capacity];
}
AbstractStringBuilder() {
this(16);
}
AbstractStringBuilder(int capacity)
:指定初始容量的构造方法。AbstractStringBuilder()
:默认初始容量为 16。
3. 常用方法
AbstractStringBuilder
提供了一些操作字符串的方法,包括 append
、insert
、delete
、replace
等。
append
:将指定的数据附加到此字符序列。
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;
}
insert
:将指定的数据插入此字符序列中。
public AbstractStringBuilder insert(int offset, String str) {
if ((offset < 0) || (offset > length()))
throw new StringIndexOutOfBoundsException(offset);
if (str == null)
str = "null";
int len = str.length();
ensureCapacityInternal(count + len);
System.arraycopy(value, offset, value, offset + len, count - offset);
str.getChars(0, len, value, offset);
count += len;
return this;
}
delete
:移除此字符序列的子字符串中的字符。
public AbstractStringBuilder delete(int start, int end) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (end > count)
end = count;
if (start > end)
throw new StringIndexOutOfBoundsException();
int len = end - start;
if (len > 0) {
System.arraycopy(value, start + len, value, start, count - end);
count -= len;
}
return this;
}
replace
:用指定的String
中的字符替换此序列的子字符串中的字符。
public AbstractStringBuilder replace(int start, int end, String str) {
if (start < 0)
throw new StringIndexOutOfBoundsException(start);
if (start > count)
throw new StringIndexOutOfBoundsException("start > length()");
if (start > end)
throw new StringIndexOutOfBoundsException("start > end");
if (end > count)
end = count;
int len = str.length();
int newCount = count + len - (end - start);
ensureCapacityInternal(newCount);
System.arraycopy(value, end, value, start + len, count - end);
str.getChars(0, len, value, start);
count = newCount;
return this;
}
4. 确保容量
AbstractStringBuilder
通过 ensureCapacityInternal
方法确保容量足够:
private void ensureCapacityInternal(int minimumCapacity) {
// overflow-conscious code
if (minimumCapacity - value.length > 0)
expandCapacity(minimumCapacity);
}
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);
}
ensureCapacityInternal
:检查当前容量是否足够,不足时调用expandCapacity
。expandCapacity
:扩展字符数组的容量,通常为当前容量的两倍加二。
5. 字符序列
AbstractStringBuilder
实现了 CharSequence
接口,因此提供了 charAt
、length
、subSequence
等方法。
charAt
:返回指定索引处的字符。
public char charAt(int index) {
if ((index < 0) || (index >= count))
throw new StringIndexOutOfBoundsException(index);
return value[index];
}
length
:返回字符序列的长度。
public int length() {
return count;
}
subSequence
:返回一个新的字符序列,它是此序列的子序列。
public CharSequence subSequence(int start, int end) {
return this.substring(start, end);
}
总结
AbstractStringBuilder
为可变字符串提供了基础实现,其子类 StringBuilder
和 StringBuffer
分别提供了非线程安全和线程安全的实现。通过理解 AbstractStringBuilder
的源码,可以更好地使用 StringBuilder
和 StringBuffer
来进行字符串操作,提高代码的性能和可读性。