Java字符串优化

发布于 2024-11-05 05:11:35 字数 138 浏览 2 评论 0原文

这会创建多少个字符串?

String test(String text) {
  return "string 1 " +
    text + " string 2 " +
    "string 3";
}

How many Strings does this create?

String test(String text) {
  return "string 1 " +
    text + " string 2 " +
    "string 3";
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

葬シ愛 2024-11-12 05:11:35

这里不存在新的 String 调用,唯一存在的调用位于 StringBuilder.toString 中,因此

1

java.lang.String test(java.lang.String);
  Code:
   Stack=3, Locals=2, Args_size=2
   0:   new #16; //class java/lang/StringBuilder
   3:   dup
   4:   ldc #18; //String string 1 
   6:   invokespecial   #20; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
   9:   aload_1
   10:  invokevirtual   #23; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   13:  ldc #27; //String  string 2 
   15:  invokevirtual   #23; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   18:  ldc #29; //String string 3
   20:  invokevirtual   #23; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   23:  invokevirtual   #31; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   26:  areturn

No new String calls are present here, the only one that exists is in StringBuilder.toString, so

1

java.lang.String test(java.lang.String);
  Code:
   Stack=3, Locals=2, Args_size=2
   0:   new #16; //class java/lang/StringBuilder
   3:   dup
   4:   ldc #18; //String string 1 
   6:   invokespecial   #20; //Method java/lang/StringBuilder."<init>":(Ljava/lang/String;)V
   9:   aload_1
   10:  invokevirtual   #23; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   13:  ldc #27; //String  string 2 
   15:  invokevirtual   #23; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   18:  ldc #29; //String string 3
   20:  invokevirtual   #23; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   23:  invokevirtual   #31; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   26:  areturn
对你的占有欲 2024-11-12 05:11:35

通过编译代码,然后使用 javap -c 检查字节码,可以很容易地回答这个问题。在这种特殊情况下,编译器应该生成这样的代码,

String result = new StringBuilder("string1").
    append(text).append("string2").append("string3").toString();

具体取决于您如何看待它,您可能会说只有一个,或者如果您是那些喜欢在使用文字时将其视为“创建”的人之一,你可以说四个。

This is pretty easy to answer by compiling the code, then inspecting the bytecode with javap -c. In this particular case, the compiler should generate code like

String result = new StringBuilder("string1").
    append(text).append("string2").append("string3").toString();

so depending on how you look at it, you might say only one, or if you're one of those people who likes to count literals as being "created" when they're used, you could say four.

〆凄凉。 2024-11-12 05:11:35

javap是你的朋友。我将您的代码放入 S.java 中,并使用 javap -c S 对其进行反汇编:

Compiled from "S.java"
public class S extends java.lang.Object{
public S();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public java.lang.String test(java.lang.String);
  Code:
   0:   new     #2; //class java/lang/StringBuilder
   3:   dup
   4:   invokespecial   #3; //Method java/lang/StringBuilder."<init>":()V
   7:   ldc     #4; //String string 1 
   9:   invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   12:  aload_1
   13:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   16:  ldc     #6; //String  string 2 
   18:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   21:  ldc     #7; //String string 3
   23:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   26:  invokevirtual   #8; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   29:  areturn

}

在 OpenJDK 1.6.0_22 中,仅创建了一个 StringBuilder 和一个 String。

javapis your friend. I put your code into S.java and deassembled it using javap -c S:

Compiled from "S.java"
public class S extends java.lang.Object{
public S();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public java.lang.String test(java.lang.String);
  Code:
   0:   new     #2; //class java/lang/StringBuilder
   3:   dup
   4:   invokespecial   #3; //Method java/lang/StringBuilder."<init>":()V
   7:   ldc     #4; //String string 1 
   9:   invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   12:  aload_1
   13:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   16:  ldc     #6; //String  string 2 
   18:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   21:  ldc     #7; //String string 3
   23:  invokevirtual   #5; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;
   26:  invokevirtual   #8; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;
   29:  areturn

}

With OpenJDK 1.6.0_22 there is only one StringBuilder and one String created.

め可乐爱微笑 2024-11-12 05:11:35

这相当于

 (new StringBuilder((new StringBuilder((new StringBuilder("String 1")).append(text).toString())).append("string 2 ").toString()).append("string 3").toString();

3 个字符串生成器和 6 个字符串。优化的方法是:

    StringBuilder sb = new StringBuilder("string 1");
    sb.append(test);
    sb.append(" string 2 ");
    sb.append("string 3");
    return sb.toString();

this is the equivalent of

 (new StringBuilder((new StringBuilder((new StringBuilder("String 1")).append(text).toString())).append("string 2 ").toString()).append("string 3").toString();

that's 3 stringbuilders and 6 strings. The way to optimize this is:

    StringBuilder sb = new StringBuilder("string 1");
    sb.append(test);
    sb.append(" string 2 ");
    sb.append("string 3");
    return sb.toString();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文