1、分析 java
对于一个字符串进行拼接有三种方法:加号、concat方法、及StringBuiler或StringBuffer。 算法
1."+"方法拼接字符串 编程
str += "c";等效于:str = new StringBuffer(str).append("c").toString(); 数组
虽然编译器对字符串加号作了优化,它会用StringBuffer的append方法进行追加。再是经过toString方法转换成String字符串的。 app
它与纯粹的append方法是不一样的: 性能
一是每次都要建立一个StringBuilder对象; 优化
二是每次执行完毕都要调用toString方法将其转换为字符串。 ui
2.concat方法拼接 this
concat方法的源码: spa
public string concat(String str){ int otherLen = str.length(); //若是追加的字符串长度为0,则返回字符串自己 if(otherLen == 0){ return this; } //字符串数组,容纳的是新字符串的字符 char buf[] = new char[count + otherLen]; //取出原字符串放到buf数组中 getChars(0, count, buf, 0); //追加的字符串转化成字符串数组,添加到buf中 str.getChars(0, otherLen, buf, count); //赋值字符数组,产生一个新的字符串 return new String(0, count + otherLen, buf); }
从总体上看就是一个数组的拷贝,虽然在内存中的处理都是原子性操做,速度很是快,注意看最后的return语句,每次的concat操做都会创建一个新的String对象,这就是concat速度慢下来的缘由。
3.appned方法拼接
StringBuilder的appned方法字节由父类的AbstractStringBuilder实现,代码以下:
public AbstractStringBuilder append(String str){ //若是是null值,则把null做为字符串处理 if(str == null)str = "null"; int len = str.length(); //字符串的长度为0,则返回自身 if(len == 0)return this; int newCount = count + len; //追加后的字符串组长度是否超过当前值 if(newCount > value.length) expandCapacity(newCount);//加长,并做数组拷贝 //字符串复制到目标数组 str.getChars(0, len, value, count); count = newCount; return this; }
整个append方法都在作字符数组处理,加长,而后数组拷贝,这些都是基本的数据处理,没有新建任何对象,因此速度也就最快了!
2、场景
看看以下代码:
public static void doWithStringBuffer(){ StringBuilder db = new StringBuilder("a"); for(int I = 0; I < 50000; I ++){ sb.append("c"); //str += "c"; //str = str.concat("c"); } String str = sb.toString(); }
1.StringBuffer的append方法执行时间是0毫秒,时间很是短暂;
2.contact方法次之,由上分析,每次的concat操做都须要建立一个String对象,它建立了5万个String对象;
3.加法拼接,每次建立一个StringBuilder对象,并执行完毕调用toString()方法转换。它建立了5万个StringBuilder对象,toString转换5万次。
3、建议
三者的实现方法不一样,性能也就不一样,可是并不表示必定要使用StringBuilder,这是由于“+”很是符合咱们的编程习惯,适合人类阅读,大多数状况下,咱们使用加号操做。
只有在系统系能临界(如在性能“增加一分则太长”的状况下)的时候,才考虑使用concat或append方法。并且不少时候系统80%的性能是消耗在20%的代码上的,咱们的精力应该更多的投入到算法和结构上。