String里面封装了char
public final class String{
/** The value is used for character storage. */
private final char value[];
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);
}
}
public class StringBuilder{
public AbstractStringBuilder append(String str) {
if (str == null)
return appendNull();
int len = str.length();
//保证char的数组能满足需求
ensureCapacityInternal(count + len);
//调用String.getChars()方法
str.getChars(0, len, value, count);
count += len;
return this;
}
}
网友评论