现在的位置: 首页 > 综合 > 正文

提高你的Java代码质量吧:让我们疑惑的字符串拼接方式的选择

2014年03月23日 ⁄ 综合 ⁄ 共 1757字 ⁄ 字号 评论关闭

一、分析 

对于一个字符串进行拼接有三种方法:加号、concat方法、及StringBuiler或StringBuffer 

1."+"方法拼接字符串 

str += "c";等效于:str =
new 
StringBuffer(str).append("c").toString();
 

虽然编译器对字符串加号做了优化,它会用StringBuffer的append方法进行追加。再是通过toString方法转换成String字符串的。 

它与纯粹的append方法是不同的 

一是每次都要创建一个StringBuilder对象 

二是每次执行完毕都要调用toString方法将其转换为字符串 

2.concat方法拼接 

concat方法的源码 

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方法都在做字符数组处理,加长,然后数组拷贝,这些都是基本的数据处理,没有新建任何对象,所以速度也就最快了 

二、场景 

看看如下代码 

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万次。 

三、建议 

三者的实现方法不同,性能也就不同,但是并不表示一定要使用StringBuilder,这是因为“+”非常符合我们的编程习惯,适合人类阅读,大多数情况下,我们使用加号操作。 

只有在系统系能临界(如在性能“增长一分则太长”的情况下)的时候,才考虑使用concat或append方法。而且很多时候系统80%的性能是消耗在20%的代码上的,我们的精力应该更多的投入到算法和结构上。

抱歉!评论已关闭.