Will this make the use of StringBuilder redundant?

Go To StackoverFlow.com

1

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");
2012-04-05 15:07
by Solid1Snake1


4

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

2012-04-05 15:11
by adarshr


4

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");
2012-04-05 15:11
by dasblinkenlight
Constant folding doesn't work for method calls. It does work with the + operator though - Alan Escreet 2012-04-10 07:24
@AlanEscreet I wrote the answer to the first edit of this question, where the OP got only two strings, not four - dasblinkenlight 2012-04-10 10:32
Still +1'd you. Your answer is absolutely correct. :- - Alan Escreet 2012-04-10 10:38


2

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.

2012-04-05 15:24
by arin


1

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.

2012-04-05 15:15
by Highland Mark


0

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.

2012-04-05 15:11
by evanwong
It's not more efficient. Due to constant folding, the + operator is more efficient for constants. The compiler cannot optimize method calls in this way, so StringBuilder will always append at runtime. Constants appended with + will be optimized in to a single String constant during compilation - Alan Escreet 2012-04-10 07:56


0

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.

2012-04-05 15:16
by random.learner


0

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/)

2012-04-10 07:19
by Alan Escreet
Ads