Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android StringBuilder vs String Concatenation

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?

like image 741
Jeremy Edwards Avatar asked Dec 18 '11 18:12

Jeremy Edwards


People also ask

Is StringBuilder better than concatenation?

It is always better to use StringBuilder. append to concatenate two string values.

Why StringBuilder is faster than string concatenation?

String is immutable whereas StringBuffer and StringBuilder are mutable classes. StringBuffer is thread-safe and synchronized whereas StringBuilder is not. That's why StringBuilder is faster than StringBuffer. String concatenation operator (+) internally uses StringBuffer or StringBuilder class.

Which is faster string concatenation or the StringBuilder class?

Note that regular string concatenations are faster than using the StringBuilder but only when you're using a few of them at a time. If you are using two or three string concatenations, use a string.

Is StringBuilder faster Java?

Objects of String are immutable, and objects of StringBuffer and StringBuilder are mutable. StringBuffer and StringBuilder are similar, but StringBuilder is faster and preferred over StringBuffer for the single-threaded program.


2 Answers

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 +.

like image 64
Ted Hopp Avatar answered Sep 30 '22 00:09

Ted Hopp


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!";
like image 21
Suragch Avatar answered Sep 29 '22 23:09

Suragch