简体   繁体   中英

Will this make the use of StringBuilder redundant?

If I do:

StringBuilder sb = new StringBuilder();
sb.append("somestring" + "hello" + "more strings" + "last one");

Is that the same as:

string s = "somestring";
s + "hello" + "more strings" + "last one";

Would it make using string builder pointless? Thereby needing to use:

StringBuilder sb = new StringBuilder();
sb.append("somestring");
sb.append("hello");
sb.append("more strings");
sb.append("last one");

The short answer to your long question is "yes".

If this is all your uses of StringBuilder in that fragment of code, it is indeed pointless. If you continue appending after the initial call to append , then it is not entirely pointless. Your last example makes a lot more sense than the first two, though.

I am assume that constants in your post are only for clarity. If they are indeed constants, the compiler will do constant folding , making the first example equivalent to

StringBuilder sb = new StringBuilder();
sb.append("somestringhello");

StringBuilder and StringBuffer should be used instead of a String "+" operation when you will be appending elements multiple times. As in:

String s = new String();
for(int i=0; i < x; i++){
  s += i;
}

Because this is going to be converted into (something along the lines of):

String s = new String();
for(int i=0; i < x; i++){
   StringBuffer sb = new StringBuffer();
   sb.append(s);
   sb.append(i)
   s = sb.toString();
}

In each iteration (within the bytecode equivalent) you are creating a StringBuffer and converting it into a String .

There are many sources but namely; this and this are OK. I have done a small research on this issue around 2010, and my knowledge on this issue dates back to that. If there are additional improvements since then I am not aware of it.

Looking at the compiled output of code that uses StringBuilder vs code that simply concatenates Strings, I find that the latest compilers will usually use StringBuilder regardless when it is appropriate. Having said that, note that in your first example, you would need to say 's = s + "hello"; in order to catch the result.

StringBuilder is mutable but String is not. StringBuilder makes these string operation more efficient. For such a small work like your example, you probably won't notice the difference tho.

In first case the fixed literal will be detected by the compiler and will be inline as a single value. like

sb.append("somestring" + "hello" + "more strings" + "last one");

will be treated as

sb.append("somestringhellomore stringslast one");

So it will append only once to StringBuilder.


In second case, it appends multiple times.

According to the String javadocs , The + operator is converted to StringBuilder anyway.

The two major differences are:

  1. When you concatenate a set of literals as you have in your example. Constant folding will translate those separate Strings in to a single String at compile time, saving this operation from being performed at run time.

    "somestring" + "hello" + "more strings" + "last one" becomes "somestringhellomore stringslast one"

  2. When you concatenate in a loop or other branching structure. Use of + will create multiple StringBuilder instances which will use more memory and processing power at run time. (see http://nicklothian.com/blog/2005/06/09/on-java-string-concatenation/ )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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