17

I was reading this documentation page, http://developer.android.com/reference/android/util/Log.html.

The section here caught my eye:

Tip: Don't forget that when you make a call like

Log.v(TAG, "index=" + i);

that when you're building the string to pass into Log.d, the compiler uses a StringBuilder and at least three allocations occur: the StringBuilder itself, the buffer, and the String object. Realistically, there is also another buffer allocation and copy, and even more pressure on the gc. That means that if your log message is filtered out, you might be doing significant work and incurring significant overhead.

This implies that the Android compiler is taking string concatenations (+) and converting them into StringBuilder and append statements.

Is my assumption correct or is it still better to use StringBuilder manually instead of string concatenation?

Jeremy Edwards
  • 14,620
  • 17
  • 74
  • 99
  • possible duplicate of [String concatenation in Java - when to use +, StringBuilder and concat](http://stackoverflow.com/questions/7817951/string-concatenation-in-java-when-to-use-stringbuilder-and-concat) – givanse Feb 03 '14 at 16:55

2 Answers2

11

The compiler does exactly what you suggest is implied. You can print the bytecodes of the generated .class file (using javap -c) and see the calls to construct and use a StringBuilder.

However, it's generally worth doing it manually when the string concatenations are spread over several lines of code. The compiler usually allocates a separate StringBuilder for every String-valued expression involving +.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 1
    @JeremyEdwards - Heh. I've said that about a lot of things. :) – Ted Hopp Dec 18 '11 at 18:44
  • 4
    **"it's generally worth doing it manually when the string concatenations are spread over several lines of code"** - I just wanted to emphasize what @TedHopp said so it isn't lost. I was concatenating a 250 character string over about 10 lines and the cpu time on App Engine was 350 msec. When I replaced this with a single StringBuilder the time was down to 50 msec, other things unchanged. – Oliver Hausler Dec 27 '14 at 20:03
3

Ted Hopp's answer is good, but it took reading it a few times to understand. Here is a rephrased answer that is hopefully more clear.

String concatenation (ie, using +, as in String myString = "Hello " + "World";) uses a StringBuilder in the background along with other allocations. Thus, for anything other than a simple one-time concatenation, it would be better to use a StringBuilder yourself.

For example,

StringBuilder myString = new StringBuilder();
myString.append("Hello ");
myString.append("to ");
myString.append("everyone ");
myString.append("in ");
myString.append("the ");
myString.append("world!");

is better than

String myString = "Hello " + "to " + "everyone " + "in " + "the " + "world!";
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • 1
    But if my string is just '"index=" + i', what's the best way? (For performance) – GMX Jan 30 '17 at 09:34
  • 1
    @GMX, That's a good question and I think it would depend on the context. If this is only being called infrequently, then using `"index=" + i` is much more readable than using a `StringBuilder`. However, if it is being called 1000 times inside of a loop and performance is an issue, then maybe some sort of `StringBuilder` option would be better. The main point of the accepted answer, though, was that when it takes multiple lines of code to build a string, then definitely use a `StringBuilder`. – Suragch Jan 30 '17 at 09:48
  • 1
    To be precise: In the StringBuilder version, you will have to add myString.toString() to get a proper String – Uglylab Dec 06 '22 at 08:59