String与StringBuilder最主要的区别体现在做字符串连接操作的效率上。
String使用重载运算符+或+=做字符串连接,有代码如下:
public class Concatenation { public static void main(String[] args) { String str = "My"; String introduction = str + " name " + " is " + " cgw "; System.out.println(introduction); } }
通过javap进行反编译,我们可以得到JVM生成的字节码:
public Concatenation(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: ldc #2; //String My 2: astore_1 3: new #3; //class java/lang/StringBuilder 6: dup 7: invokespecial #4; //Method java/lang/StringBuilder."<init>":()V 10: aload_1 11: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 14: ldc #6; //String name 16: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 19: ldc #7; //String is 21: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 24: ldc #8; //String cgw 26: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 29: invokevirtual #9; //Method java/lang/StringBuilder.toString:()Ljava/lang/String; 32: astore_2 33: getstatic #10; //Field java/lang/System.out:Ljava/io/PrintStream; 36: aload_2 37: invokevirtual #11; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 40: return }
我们可以看到,在main方法内,JVM是new了一个StringBuilder对象,并通过调用append方法进行字符串连接操作的。
既然这样,那么我们可以不考虑使用StringBuilder对象,而直接使用String,让JVM来为你自动进行优化呢?
请看下面代码:
import java.lang.StringBuilder; public class WhitherStringBuilder { public String implicit(String[] fields) { String result = ""; for(int i = 0 ; i < fields.length ; i++) { result += fields[i]; } return result; } public String explicit(String[] fields) { StringBuilder result = new StringBuilder(); for(int i = 0 ; i < fields.length ; i++) { result.append(fields[i]); } return result.toString(); } }
同样进行反编译得到:
public class WhitherStringBuilder extends java.lang.Object{ public WhitherStringBuilder(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public java.lang.String implicit(java.lang.String[]); Code: 0: ldc #2; //String 2: astore_2 3: iconst_0 4: istore_3 5: iload_3 6: aload_1 7: arraylength 8: if_icmpge 38 11: new #3; //class java/lang/StringBuilder 14: dup 15: invokespecial #4; //Method java/lang/StringBuilder."<init>":()V 18: aload_2 19: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 22: aload_1 23: iload_3 24: aaload 25: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 28: invokevirtual #6; //Method java/lang/StringBuilder.toString:()Ljava/lang/String; 31: astore_2 32: iinc 3, 1 35: goto 5 38: aload_2 39: areturn public java.lang.String explicit(java.lang.String[]); Code: 0: new #3; //class java/lang/StringBuilder 3: dup 4: invokespecial #4; //Method java/lang/StringBuilder."<init>":()V 7: astore_2 8: iconst_0 9: istore_3 10: iload_3 11: aload_1 12: arraylength 13: if_icmpge 30 16: aload_2 17: aload_1 18: iload_3 19: aaload 20: invokevirtual #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 23: pop 24: iinc 3, 1 27: goto 10 30: aload_2 31: invokevirtual #6; //Method java/lang/StringBuilder.toString:()Ljava/lang/String; 34: areturn }
注意:
implicit方法为使用String的+操作进行字符串连接,观察字节码第8-35行,这是一个循环,你会发现,new StringBuilder对象的操作是在循环体内进行的,这说明每次循环都会创建一个新的StringBuilder对象。
explicit方法为使用StringBuilder对象的append方法进行字符串连接,JVM在第0行创建了StringBuilder对象,而循环是在第13至27行,这说明,使用StringBuilder进行字符串连接只会创建一个StringBuilder对象。
由以上得知StringBuilder的字符串连接操作效率要远远高于String。
另StringBuilder的效率要稍高于StringBuffer,因为StringBuffer的线程安全特性会造成额外的开销。