StringBuider,StringBuffer,String源码部分方法的比较
1.StringBuider类
StringBuider中append(String)的实现
StringBuider继承自AbstractStringBuilder ,
AbstractStringBuilder中有两个全局属性分别是:
char value[]; //存储字符的数组
int count; //数组中,已经存储字符的个数。(不是数组的长度)
来看一下append(String)的实现:
public String concat(String str) {int otherLen = str.length();if (otherLen == 0) {return this;}char buf[] = new char[count + otherLen];getChars(0, count, buf, 0);str.getChars(0, otherLen, buf, count);return new String(0, count + otherLen, buf);} ?
可以看到在String类型的字符串后面增加字符串,会new一个新的String返回。这里如果不处理好会发生内存泄露。而StringBuffer和StringBuider的append(String)就不存在这个情况。
?