字符串拼接详解

1.使用 + 拼接字符串详解
// 源码
public class Demo02 {
    public static void main(String[] args) {
        String sb = new String();
        for (int i = 0; i < 10; i ++){
            sb += i;
        }
        System.out.println(sb);
    }
}

// 字节码(javac Demo02.java,javap -c Demo02.class)
Compiled from "Demo02.java"
public class Demo02 {
  public Demo02();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class java/lang/String
       3: dup
       4: invokespecial #3                  // Method java/lang/String."":()V
       7: astore_1
       8: iconst_0
       9: istore_2
      10: iload_2
      11: bipush        10
      13: if_icmpge     41
      16: new           #4                  // class java/lang/StringBuilder
      19: dup
      20: invokespecial #5                  // Method java/lang/StringBuilder."":()V
      23: aload_1
      24: invokevirtual #6                  // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      27: iload_2
      28: invokevirtual #7                  // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      31: invokevirtual #8                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      34: astore_1
      35: iinc          2, 1
      38: goto          10
      41: getstatic     #9                  // Field java/lang/System.out:Ljava/io/PrintStream;
      44: aload_1
      45: invokevirtual #10                 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      48: return
}
2.使用StringBuilder拼接字符串详解
// 源码
public class Demo01 {
    public static void main(String[] args) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 10; i ++){
            sb.append(i);
        }
        System.out.println(sb.toString());
    }
}

// 字节码(javac Demo01.java,javap -c Demo01.class)
Compiled from "Demo01.java"
public class Demo01 {
  public Demo01();
    Code:
       0: aload_0
       1: invokespecial #1                  // Method java/lang/Object."":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: new           #2                  // class java/lang/StringBuilder
       3: dup
       4: invokespecial #3                  // Method java/lang/StringBuilder."":()V
       7: astore_1
       8: iconst_0
       9: istore_2
      10: iload_2
      11: bipush        10
      13: if_icmpge     28
      16: aload_1
      17: iload_2
      18: invokevirtual #4                  // Method java/lang/StringBuilder.append:(I)Ljava/lang/StringBuilder;
      21: pop
      22: iinc          2, 1
      25: goto          10
      28: getstatic     #5                  // Field java/lang/System.out:Ljava/io/PrintStream;
      31: aload_1
      32: invokevirtual #6                  // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      35: invokevirtual #7                  // Method java/io/PrintStream.println:(Ljava/lang/String;)V
      38: return
}

总结

对比字节码,我们会发现 + 拼接已经用StringBuilder优化,但是 + 每拼接一次就会比直接用StringBuilder多创建一个对象多调用一次toString,效率比较低创建多余对象,所以还是推荐使用StringBuilder。

你可能感兴趣的:(字符串拼接详解)