9

I've heard that the compiler (or was it the JVM?) will automatically use a StringBuilder for some string concatenation. When is the right time to explicitly declare one? I don't need a StringBuffer for being thread-safe.

Thanks.

Jon Onstott
  • 13,499
  • 16
  • 80
  • 133
  • 1
    possible duplicate of [What happens when Java Compiler sees many String concatenations in one line?](http://stackoverflow.com/questions/1296571/what-happens-when-java-compiler-sees-many-string-concatenations-in-one-line) – Paul Creasey Nov 23 '10 at 23:37
  • 1
    Note: one situation where its won't use a StringBuilder is when the string can be concatenated at Compile time. In which case it just produces one lone string (no StringBuilder) – Peter Lawrey Nov 24 '10 at 09:28
  • BTW: If you ever need to use StringBuffer for thread safety, you have a design issue IMHO. :P – Peter Lawrey Nov 24 '10 at 09:28

1 Answers1

15

The compiler will use it automatically for any string concatenation using "+".

You'd usually use it explicitly if you wanted to concatenate in a loop. For example:

StringBuilder builder = new StringBuilder();
for (String name : names)
{
    builder.append(name);
    builder.append(", ");
}
if (builder.length() > 0)
{
    builder.setLength(builder.length() - 2);
}
System.out.println("Names: " + builder);

Another situation would be where you wanted to build up a string over multiple methods, or possibly conditionalise some bits of the building. Basically, if you're not building the string in a single statement (where the compiler can help you) you should at least consider using StringBuilder.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    I think it's worth mentioning that `StringBuilder` will _not_ be used in cases like this: `String str = ""; str += `. – Matt Ball Nov 23 '10 at 23:38
  • Ah that makes sense, thanks! BTW I thought you were a C# guy ;o) – Jon Onstott Nov 23 '10 at 23:38
  • 1
    @Jon: C# is certainly my *preferred* language - but I've written more professional code in Java than C# :) – Jon Skeet Nov 23 '10 at 23:39
  • @Matt: Please give a fuller example. I've just tried it, and it *does* use `StringBuilder` with the compiler I'm using (JDK 7). In theory it could use `String.concat` instead, but I haven't seen any evidence of that happening. Of course, the exact implementation isn't specified, but I can't remember seeing any compiler use `String.concat`. – Jon Skeet Nov 23 '10 at 23:40
  • @Jon Are you looking at IL/bytecode when you see that StringBuilder is being used? – Jon Onstott Nov 23 '10 at 23:42
  • @Matt, this Sun [blog post](http://blogs.sun.com/dholmes/entry/minimize_garbage_generation) suggests the opposite. Specifically, `a` is not a compile-time constant. – Matthew Flaschen Nov 23 '10 at 23:43
  • 1
    @Jon: sorry, I realized that comment was less-than-clear. [This answer](http://stackoverflow.com/questions/2721998/how-java-do-the-string-concatenation-using/2722062#2722062) illustrates what I meant (read: failed) to say. `StringBuilder` will be used, but in the concatenation in my first comment, it would produce bytecode equivalent to `new StringBuilder().append("foo").append().toString();`. Bah, I should have initialized `str` to a non-empty string in that comment, too. – Matt Ball Nov 23 '10 at 23:48
  • @Matthew: see my 2nd comment to J.S. - communication fail on my part. – Matt Ball Nov 23 '10 at 23:51
  • @Matt: Right, yes. It will use a new instance of `StringBuilder` each time the statement is executed. – Jon Skeet Nov 24 '10 at 06:26