首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

StringBuider,StringBuffer,String源码一部分方法的比较

2012-10-24 
StringBuider,StringBuffer,String源码部分方法的比较1.StringBuider类StringBuider中append(String)的实

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)就不存在这个情况。

?

热点排行