字符串串联是否经过优化以使用现有的 StringBuilders?

2022-09-01 23:16:32

我有以下代码:

StringBuilder str = new StringBuilder("foo");
for(Field f : fields){
    str.append("|" + f);
}
str.append("|" + bar);
String result = str.toString();

我知道编译器会优化字符串串联并将其替换为StringBuilder。但是,会创建新的StringBuilder还是在Java 8中使用现有的StringBuilder?Java 9怎么样?"|" + fstr


答案 1

默认情况下,在java-9中,字符串串联将没有;这是一个运行时决策,如何通过 .默认策略不是一个。StringBuilderinvokedynamicStringBuilder::append

您也可以在此处阅读更多内容

在java-8下,将创建一个新的(在解编译的字节码中很容易发现两个实例。invokespecial // Method java/lang/StringBuilder."<init>":()V

另外,您有关于 的建议 ;只是注意到这比 好得多,这就是原因。append.append...sb.append ... sb.append


答案 2

由于字符串串联优化由 Java 编译器执行,因此您可以通过反编译字节码来了解它的作用:

$ cat Test.java
interface Field {}

public class Test {

    static String toString(Field[] fields, Object bar) {
        StringBuilder str = new StringBuilder("foo");
        for(Field f : fields){
            str.append("|" + f);
        }
        str.append("|" + bar);
        return str.toString();
    }
}
$ javac Test.java
$ javap -c Test.class
Compiled from "Test.java"
public class stackoverflow.Test {
  public stackoverflow.Test();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  static java.lang.String toString(stackoverflow.Field[], java.lang.Object);
    Code:
       0: new           #16                 // class java/lang/StringBuilder
       3: dup
       4: ldc           #18                 // String foo
       6: invokespecial #20                 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
       9: astore_2
      10: aload_0
      11: dup
      12: astore        6
      14: arraylength
      15: istore        5
      17: iconst_0
      18: istore        4
      20: goto          53
      23: aload         6
      25: iload         4
      27: aaload
      28: astore_3
      29: aload_2
      30: new           #16                 // class java/lang/StringBuilder
      33: dup
      34: ldc           #23                 // String |
      36: invokespecial #20                 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
      39: aload_3
      40: invokevirtual #25                 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
      43: invokevirtual #29                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      46: invokevirtual #32                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      49: pop
      50: iinc          4, 1
      53: iload         4
      55: iload         5
      57: if_icmplt     23
      60: aload_2
      61: new           #16                 // class java/lang/StringBuilder
      64: dup
      65: ldc           #23                 // String |
      67: invokespecial #20                 // Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
      70: aload_1
      71: invokevirtual #25                 // Method java/lang/StringBuilder.append:(Ljava/lang/Object;)Ljava/lang/StringBuilder;
      74: invokevirtual #29                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      77: invokevirtual #32                 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
      80: pop
      81: aload_2
      82: invokevirtual #29                 // Method java/lang/StringBuilder.toString:()Ljava/lang/String;
      85: areturn
}

如您所见,代码在 3 个位置调用 StringBuilder 构造函数 (),因此将在每次迭代中创建新的 StringBuilders(除非实时编译器执行花哨的优化)。Method java/lang/StringBuilder."<init>":

这不太可能是一个重大的性能问题,但在极少数情况下,您可以通过重写到

str.append("|").append(f);

推荐