LZ,源码在这: /** * Appends the specified string to this character sequence. * <p> * The characters of the <code>String</code> argument are appended, in * order, increasing the length of this sequence by the length of the * argument. If <code>str</code> is <code>null</code>, then the four * characters <code>"null"</code> are appended. * <p> * Let <i>n</i> be the length of this character sequence just prior to * execution of the <code>append</code> method. Then the character at * index <i>k</i> in the new character sequence is equal to the character * at index <i>k</i> in the old character sequence, if <i>k</i> is less * than <i>n</i>; otherwise, it is equal to the character at index * <i>k-n</i> in the argument <code>str</code>. * * @param str a string. * @return a reference to this object. */ public AbstractStringBuilder append(String str) { if (str == null) str = "null";//此时直接把其当成“null”串来处理 int len = str.length();//取得str的长度 if (len == 0) return this;//Str=""时,可不再作处理,直接返回原对象 int newCount = count + len;//当前串的长度(newCount) = 原有串长(count) + str的长度(用于更新StringBuilder的count属性值) if (newCount > value.length)//当前对象的长度大于其容量的条件下增加 <span style="color: #000000;"> <span>expandCapacity(newCount)</span>;//扩展当前的对象的容量,具体见下面 <span>str.getChars(0, len, value, count);</span>//把串的值依次插入到当前对象有末端(具体如下) count = newCount;//更新当前对象的容量属性</span> return this;//返回当前对象(对象有内容和属性已修改) }
和Java 8 还有一点不一样呢 更简洁了 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; }
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; } 一个动态字符串的拼接。
/**
* Appends the specified string to this character sequence.
* <p>
* The characters of the <code>String</code> argument are appended, in
* order, increasing the length of this sequence by the length of the
* argument. If <code>str</code> is <code>null</code>, then the four
* characters <code>"null"</code> are appended.
* <p>
* Let <i>n</i> be the length of this character sequence just prior to
* execution of the <code>append</code> method. Then the character at
* index <i>k</i> in the new character sequence is equal to the character
* at index <i>k</i> in the old character sequence, if <i>k</i> is less
* than <i>n</i>; otherwise, it is equal to the character at index
* <i>k-n</i> in the argument <code>str</code>.
*
* @param str a string.
* @return a reference to this object.
*/
public AbstractStringBuilder append(String str) {
if (str == null) str = "null";//此时直接把其当成“null”串来处理
int len = str.length();//取得str的长度
if (len == 0) return this;//Str=""时,可不再作处理,直接返回原对象
int newCount = count + len;//当前串的长度(newCount) = 原有串长(count) + str的长度(用于更新StringBuilder的count属性值)
if (newCount > value.length)//当前对象的长度大于其容量的条件下增加
<span style="color: #000000;"> <span>expandCapacity(newCount)</span>;//扩展当前的对象的容量,具体见下面
<span>str.getChars(0, len, value, count);</span>//把串的值依次插入到当前对象有末端(具体如下)
count = newCount;//更新当前对象的容量属性</span>
return this;//返回当前对象(对象有内容和属性已修改)
}
和Java 8 还有一点不一样呢
更简洁了 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;
}
if (str == null)
return appendNull();
int len = str.length();
ensureCapacityInternal(count + len);
str.getChars(0, len, value, count);
count += len;
return this;
}
一个动态字符串的拼接。