I found an interesting answer to compare String vs StringBuffer performance from Reggie Hutcherso Source : http://www.javaworld.com/javaworld/jw-03-2000/jw-0324-javaperf.html
Java provides the StringBuffer and String classes, and the String class is used to control character strings that cannot be changed. Simply put, objects of type String are read only and immutable. The StringBuffer class is used to represent characters that can be changed.
The significant performance difference between the two classes is that StringBuffer is faster than String when doing simple concatenations. In String manipulation code, character strings are usually concatenated. Using the String class, concatenations are usually performed as follows:
String str = new String ("Stanford "); str += "Lost!!";
If you want to use a StringBuffer to perform the same concatenation, you will need code that looks like this:
StringBuffer str = new StringBuffer ("Stanford "); str.append("Lost!!");
Developers usually assume that the first example above is more efficient, because they believe that the second example, which uses the append method to concatenate, is more expensive than the first example, which uses the + operator to concatenate two String objects.
The + operator seems innocent, but the generated code causes some surprises. Using StringBuffer to concatenate can actually produce code that is significantly faster than using String. To find out why this is so, we need to examine the generated bytecode from our two examples. The byte code for an example using String is as follows:
0 new #7 <Class java.lang.String> 3 dup 4 ldc #2 <String "Stanford "> 6 invokespecial #12 <Method java.lang.String(java.lang.String)> 9 astore_1 10 new #8 <Class java.lang.StringBuffer> 13 dup 14 aload_1 15 invokestatic #23 <Method java.lang.String valueOf(java.lang.Object)> 18 invokespecial #13 <Method java.lang.StringBuffer(java.lang.String)> 21 ldc #1 <String "Lost!!"> 23 invokevirtual #15 <Method java.lang.StringBuffer append(java.lang.String)> 26 invokevirtual #22 <Method java.lang.String toString()> 29 astore_1
The bytecode in places 0 through 9 is executed for the first line of code, namely:
String str = new String("Stanford ");
Then the bytecode in places 10 through 29 is executed to concatenate:
str += "Lost!!";
It is interesting here. The bytecode generated for concatenation creates a StringBuffer object, then calls its append method: a temporary StringBuffer object is created at location 10, and its append method is called at location 23. Since the String class is immutable, StringBuffer should be used for concatenation.
After concatenation is performed on the StringBuffer object, it must be converted back to String. This is done by calling the toString method at location 26. This method creates a new String object from a temporary StringBuffer object. Creating this temporary StringBuffer object and then converting it back to a String object is very expensive.
Thus, the two lines of code above lead to the creation of three objects:
- String object at location 0
- StringBuffer at location 10
- String object at location 26
Now let's look at the bytecode generated for the example using StringBuffer:
0 new #8 <Class java.lang.StringBuffer> 3 dup 4 ldc #2 <String "Stanford "> 6 invokespecial #13 <Method java.lang.StringBuffer(java.lang.String)> 9 astore_1 10 aload_1 11 ldc #1 <String "Lost!!"> 13 invokevirtual #15 <Method java.lang.StringBuffer append(java.lang.String)> 16 pop
The bytecode in places 0 through 9 is executed for the first line of code:
StringBuffer str = new StringBuffer("Stanford ");
Then, to concatenate, the bytecode at position 10-16 is executed:
str.append("Lost!!");
Note that, as in the first example, this code calls the method to add a StringBuffer object. However, unlike the first example, there is no need to create a temporary StringBuffer and then convert it to a String object. This code creates only one object, StringBuffer, at location 0.
In conclusion, StringBuffer concatenation is significantly faster than string concatenation. Obviously, StringBuffers should be used in this type of operation whenever possible. If String functionality is required, consider using a StringBuffer to concatenate, and then do one conversion to String.