使用+符号进行字符串连接 [英] String concatenation with the + symbol

查看:212
本文介绍了使用+符号进行字符串连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我正在阅读 Antonio关于toString的博客()性能并且有一段:

Today I was reading Antonio's Blog about toString() performance and there is a paragraph:


昨天曾经被认为是邪恶的(不要用+ !!连接字符串! !),变得冷静和高效! 今天,JVM将+符号编译为字符串构建器(在大多数情况下)。所以,不要犹豫,使用它。

What used to be considered evil yesterday ("do not concatenate Strings with + !!!"), has become cool and efficient! Today the JVM compiles the + symbol into a string builder (in most cases). So, do not hesitate, use it.

现在我很困惑,因为他说今天JVM将+符号编译成字符串构建器(在大多数情况下) ),但我以前从未听过或看过(代码)这样​​的事情。

Now I am confused, because he is saying Today the JVM compiles the + symbol into a string builder (in most cases), but I have never heard or seen(code) anything like this before.

有人可以举例说明 JVM在哪做以及它在什么条件下发生

推荐答案

规则


不要使用+ !!!连接字符串

"do not concatenate Strings with + !!!"

错误,因为它不完整,因此具有误导性。

is wrong, because it is incomplete and therefore misleading.

规则是


不要在循环中将字符串与+ 连接

并且该规则仍然有效。原始规则从未打算在循环之外应用!

and that rule still holds. The original rule was never meant to be applied outside of loops!

一个简单的循环

String s = "";
for (int i = 0; i < 10000; i++) { s += i; }
System.out.println(s);

仍远低于

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10000; i++) { sb.append(i); }
System.out.println(sb.toString());

因为Java编译器必须将第一个循环转换为

because the Java compiler has to translate the first loop into

String s = "";
for (int i = 0; i < 1000; i++) { s = new StringBuilder(s).append(i).toString(); }
System.out.println(s);

此外还有索赔


今天,JVM将+符号编译成字符串构建器(在大多数情况下)。

Today the JVM compiles the + symbol into a string builder (in most cases).

至少具有误导性,因为这个翻译已经用Java 1.0完成了(好吧,不是用StringBuilder而是用StringBuffer,因为StringBuilder只是用Java5添加的)。

is misleading at least, because this translation was already done with Java 1.0 (ok, not with StringBuilder but with StringBuffer, because StringBuilder was only added with Java5).

人们还可以争辩说索赔


今天,JVM将+符号编译成字符串构建器(在大多数情况下)。 / p>

Today the JVM compiles the + symbol into a string builder (in most cases).

完全错误,因为编译不是由JVM完成的。它由Java编译器完成。

is simply wrong, because the compilation is not done by the JVM. It is done by the Java Compiler.

关于问题:Java编译器何时使用 StringBuilder.append()什么时候使用其他机制?

For the question: when does the Java compiler use StringBuilder.append() and when does it use some other mechanism?

Java编译器的源代码(版本1.8)包含两个地方其中字符串通过 + 运算符进行处理。

The source code of the Java compiler (version 1.8) contains two places where String concationation through the + operator is handled.

  • the first place is String constant folding (http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/com/sun/tools/javac/comp/ConstFold.java?av=f#314). In this case the compiler can calculate the resulting string and works with the resulting string.
  • the second place is where the compiler creates the code for assignment operations (http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/com/sun/tools/javac/jvm/Gen.java?av=f#2056). In this case the compiler always emits code to create a StringBuilder

结论对于来自OpenJDK的Java编译器(这意味着由Oracle分发的编译器),在大多数情况下这个短语意味着始终。 (虽然这可能会随着Java 9而改变,或者可能是另一个像Eclipse中包含的Java编译器使用其他机制的Java编译器。)

The conclusion is that for the Java compiler from the OpenJDK (which means the compiler distributed by Oracle) the phrase in most cases means always. (Though this could change with Java 9, or it could be that another Java compiler like the one that is included within Eclipse uses some other mechanism).

这篇关于使用+符号进行字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆