2

I understand that StringBuilder should be used for concatenating multiple strings rather than using +. My question is what is the cut off point?

I have been told that if concatenating 4 or more strings you should use the StringBuilder.append(), and for anything else, use +. Is that the case? or is the point at which stringbuilder is more efficient more than 4?

Pectus Excavatum
  • 3,593
  • 16
  • 47
  • 68

6 Answers6

2

As of Java 1.5, the compiler automatically uses StringBuilder when + is used in the source.

From the Javadoc for String:

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method.

ach
  • 6,164
  • 1
  • 25
  • 28
2

Zero.

Many years ago, in a Java version far far away (1.4), some people recommended replacing concatenation by StringBuffer (the thread-safe equivalent of StringBuilder, which had not been invented yet).

With the introduction of StringBuilder, and the redefinition of concatenation in terms of the faster StringBuilder, "optimized" code using StringBuffer incurred in unneeded synchronization, while non-optimal code with + was automatically enjoyed to the benefits of StringBuilder.

I would use StringBuilder when the concatenation is mixed with control structures or loops, or when I need to update some characters in the middle of the strings. Otherwise I assume that the compiler is smart enough to do a good job with the traditional concatenation.

Javier
  • 12,100
  • 5
  • 46
  • 57
1

Starting in jse 5, the + sign converts to a stringbuilder during compilation.

There is a caveat to this though, this:

String blam = a + b + c + d + e;

results in one stringbuilder and 5 appends (as expected)

This; however:

String blam = a;
blam += b + c;
blam := d + e;

results in 3 stringbuilders (one per line).

The point: + sign is fine, just stack it all in one line of code.

DwB
  • 37,124
  • 11
  • 56
  • 82
  • 1
    And in fact, in loops it makes a string builder for each concat, which could be a lot. In the case of catting in a loop, you should probably define the builder at the top of it and append on each iteration. – Cruncher Mar 19 '13 at 15:02
  • Correct. do not do string concats inside loops. – DwB Mar 19 '13 at 15:04
0

There is no defined answer because it's depends on JVM implementation. I use StringBuilder when I need to concat more than 3 strings.

I noticed a good case. When you need to concat static final String's you don't have to use StringBuilder. The compliler will concat it withour performance falling:

final class Example {

    public static final String STRING_ONE = "string";
    public static final String STRING_TWO = "string";
    public static final String STRING_THREE = "string";
    public static final String STRING_FOUR = "string";

    public String getBigString() {
        return STRING_ONE + STRING_TWO + STRING_THREE + STRING_FOUR;    
    } 

}

Also I very interested in that question. I tryed to implement a good test and asked about that here.

Community
  • 1
  • 1
Pavel
  • 4,912
  • 7
  • 49
  • 69
0

I would say that the compiler will substitute the plus with StringBuilder itself.

There are several situations you want to avoid in this case. For example using + in a loop. For each iteration a new StringBuilder will be created.

Might wanna read this question

Community
  • 1
  • 1
WeMakeSoftware
  • 9,039
  • 5
  • 34
  • 52
0

The compiler is pretty good at optimizing this stuff, so usually, you won't gain much. But if you're doing concatenations in a loop for instance, you might gain some benefits from using a StringBuilder instead.

NilsH
  • 13,705
  • 4
  • 41
  • 59