public synchronized StringBuffer append(String str) {
toStringCache = null;
super.append(str);
return this;
}
public StringBuilder append(String str) {
super.append(str);
return this;
}
缓冲区
/**
* A cache of the last value returned by toString. Cleared
* whenever the StringBuffer is modified.
* toString返回的最后一个值的缓存。 每当修改StringBuffer时清除。
*/
private transient String toStringCache;
public synchronized String toString() {
if (toStringCache == null) {
return toStringCache =
isLatin1() ? StringLatin1.newString(value, 0, count)
: StringUTF16.newString(value, 0, count);
}
return new String(toStringCache);
}
public String toString() {
// Create a copy, don‘t share the array
// 复制一个数组,但是不分享这个数组
return isLatin1() ? StringLatin1.newString(value, 0, count)
: StringUTF16.newString(value, 0, count);
}
原文:https://www.cnblogs.com/IAoli/p/12574281.html