简体   繁体   English

使用+在Java中连接字符串

[英]Using + to concat string in Java

I have a huge string as below : 我有一个巨大的字符串如下:

String str = "aaaaaaa" 
        + "bbbbbbbb" 
        + "cccccccc" 
    .... 

        + "zzzzzzzz";  // about 200 lines long

Is it a big waste of memory? 这是对记忆的大浪费吗? Will it be better to put it in one line or use StringBuilder ? 将它放在一行或使用StringBuilder会更好吗? This is done to create a long sql. 这样做是为了创建一个长sql。

No. The compiler will perform the concatenation at compile time if all your strings are constant (as they are in your question). 不会。如果所有字符串都是常量的,编译器将在编译时执行连接(因为它们在您的问题中)。 So that ends up performing better than the StringBuilder version. 因此最终比StringBuilder版本表现更好

It will use StringBuilder (or StringBuffer ) to perform the concatenation of any non-constant parts otherwise. 它将使用StringBuilder (或StringBuffer )来执行任何非常量部分的串联。

So the compiled version of: 所以编译版本:

String x = "a" + "b";

should be exactly the same as: 完全相同

String x = "ab";

Note that the "non-constant" parts can mean that you can sometimes get (very minor) extra efficiencies using bracketing wisely. 请注意,“非常量”部分可能意味着您有时可以明智地使用包围来获得(非常小的)额外效率。 For example: 例如:

int value = ...;
String x = "a" + value + "b" + "c";

... is less efficient than: ......效率低于:

int value = ...;
String x = "a" + value + ("b" + "c");

... as the first appends "b" and "c" separately. ......因为第一个分别附加"b""c" The efficient version is equivalent tO: 高效版本相当于:

int value = ...;
String x = "a" + value + "bc";

I can't remember ever seeing this used as a legitimate micro-optimization, but it's at least a point of interest. 我不记得曾经看到这被用作合法的微优化,但它至少是一个兴趣点。

In current versions of Java, each + (concatenation) operation gets automatically compiled to a StringBuilder append() operation. 在当前版本的Java中,每个+ (连接)操作都会自动编译为StringBuilder append()操作。 However, if it's inside a loop, it will create a new StringBuilder for each iteration, negating its effect - so you'd be better of managing the StringBuilder explicitly. 但是,如果它在循环中,它将为每次迭代创建一个新的StringBuilder ,否定它的影响 - 所以你最好明确地管理StringBuilder

No. The compiler will optimise this for you into a StringBuilder . 不会。编译器会为您优化这个StringBuilder

Edit: As Louis Wasserman points out, it will also combine String literals for you, so the statement above would just be a single String at compile time. 编辑:正如Louis Wasserman所指出的,它也会为你组合字符串文字,所以上面的语句在编译时只是一个字符串。

AFAIK JVM internally has a pool of Strings, every String you create is stored there, discarding the oldest/least used ones (im not sure how it chooses which ones). AFAIK JVM内部有一个字符串池,你创建的每个字符串都存储在那里,丢弃最旧/最少使用的字符串(我不知道它是如何选择的)。 If you explictly invoke String.intern() youre telling the JVM to put the String in the pool. 如果你明确地调用String.intern(),则告诉JVM将String放入池中。

when you concatenate two Strings using + you end up with three Strings in the pool. 当你使用+连接两个字符串时,你最终会在池中使用三个字符串。 If you concatenate a + b + c, you woyld end up with 5 Strings (a, b, ab, c, abc) 如果你连接a + b + c,你最终会得到5个字符串(a,b,ab,c,abc)

At least thats what i recall on older JVM versions, but Luiggi is quite probably right, that on newer ones its internally optimized 至少这就是我在旧的JVM版本上所记得的,但是Luiggi很可能是正确的,在内部优化的新版本上

The code: 代码:

String str = "aaaaaaa" + "bbbbbbbb" + "cccccccc";

gets transformed at compile time to: 在编译时转换为:

String str = (new StringBuilder()).append("aaaaaaa").append("bbbbbbbb").append("cccccccc").toString();

So, there doesn't appear to be any difference in using StringBuilder. 因此,使用StringBuilder似乎没有任何区别。

There's a very nice article that closely compares the performance of concat (+) against StringBuilder and StringBuffer, complete with line graphs: 有一篇非常好的文章将concat(+)与StringBuilder和StringBuffer的性能进行了比较,并附有折线图:

StringBuilder vs StringBuffer vs String.concat - Done Right . StringBuilder vs StringBuffer vs String.concat - Done Right

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM