具体细节可能稍有取决于 JDK 版本,但取决于我的本地版本 1.8.0_66:
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;
}
private void ensureCapacityInternal(int minimumCapacity) {
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);
}
请注意,value.length 实际上是容量,而不是正在存储的字符串的长度。当前缓冲区中的字符数为count!还记得在调用new StringBuffer() 时,value.length 最初是 16。考虑到这些,让我们对您介绍的每个案例进行一点堆栈跟踪。
对于大小为 17 的字符串:
sb.append("12345678901234567")
if (str == null) -> false
len = 17;
ensureCapacityInternal(0 + 17)
if (17 - 16 > 0) -> true
expandCapacity(17)
newCapacity = 16 * 2 + 2 = 34
if (34 - 17 < 0) -> false
value = Arrays.copyOf("", 34)
str.getChars(0, 17, "", 17)
return this
sb.build() -> "12345678901234567"
sb.capacity() -> 34
对于大小为 35 的字符串:
sb.append("12345678901234567890123456789012345")
if (str == null) -> false
len = 35;
ensureCapacityInternal(0 + 35)
if (35 - 16 > 0) -> true
expandCapacity(35)
newCapacity = 16 * 2 + 2 = 34
if (34 - 35 < 0) -> true
newCapacity = 35
value = Arrays.copyOf("", 35)
str.getChars(0, 35, "", 35)
return this
sb.build() -> "12345678901234567890123456789012345"
sb.capacity() -> 35
请注意,区别在于if (newCapacity - minimumCapacity < 0) 行。如果附加的字符串长度超过oldCapacity * 2 + 2,则newCapacity 将设置为要附加的字符串的长度。
换句话说,在追加时,如果缓冲区不足以容纳现有文本和追加的文本,它会检查(大致)加倍大小是否能容纳新文本。如果这仍然不够,而不是递归扩展,它将扩展到完全足够大。
这不仅发生在 35 上,尽管字符串比这长得多,但您可能不会遇到您追加的内容是当前容量的两倍以上的情况。
如果你这样做,你也会看到相同的“长度 = 容量”
StringBuffer sBuffer = new StringBuffer();
sBuffer.append("1234567890123456");
System.out.println(sBuffer.capacity()); // 16
sBuffer.append("1234567890123456789");
System.out.println(sBuffer.capacity()); // 35
但不是
StringBuffer sBuffer = new StringBuffer();
sBuffer.append("1234567890123456");
System.out.println(sBuffer.capacity()); // 16
sBuffer.append("123456789012345678");
System.out.println(sBuffer.capacity()); // 34
sBuffer.append("1");
System.out.println(sBuffer.capacity()); // 70