Java provides StringBuffer
String
the and classes, and the String
class is used to manipulate character strings that cannot be Chang Ed. Simply stated, objects of type is read only and String
immutable. The StringBuffer
class is used to represent characters, can be modified.
The significant performance difference between these both classes is that's StringBuffer
faster than when String
performing simple Concatenations. String
in manipulation code, character strings is routinely concatenated. Using String
The class, concatenations is typically performed as follows:
String=newstring("Stanford"); + ="lost!!" ;
If you were StringBuffer
to perform the same concatenation, you would need code that looks like this:
StringBuffer=newstringbuffer("Stanford"); Str. Append("lost!!" );
Developers usually assume that the first example above are more efficient because they think that the second example, which Uses the append
method for concatenation, was more costly than the first example, which uses the +
operator to Concat Enate, String
objects.
+
the operator appears innocent, but the code generated produces some surprises. Using a for StringBuffer
concatenation can in fact produce code, is significantly faster than using a String
. To discover what the case, we must examine the generated bytecode from our examples. The bytecode for the example using String
looks like this:
0 New #7 <class java.lang.string>3Dup4Ldc#2 <string "Stanford" >6Invokespecial#12 <method java.lang.String (java.lang.String) >9Astore_110 New #8 <class java.lang.stringbuffer>13 Dup14 Aload_115 #23 <method java.lang.String valueOf (java.lang.Object) >< Span class= "lit" >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 #22 <method java.lang.String toString () > 29 astore_1
The bytecode at locations 0 through 9 are executed for the first line of code, namely:
String=newstring("Stanford");
Then, the bytecode at location through are executed for the concatenation:
+ ="lost!!" ;
Things get interesting here. The bytecode generated for the concatenation creates a stringbuffer
object and then invokes its append
method:the temporary stringbuffer
object is created at location 10, and its append
method is called in Location 23. Because the String
class is immutable, a stringbuffer
must being used for Concatenation.
After the concatenation was performed on StringBuffer
the object, it must was converted back into a String
. This is do with the call to the toString
method at location 26. This method creates a new String
object from the temporary StringBuffer
object. The creation of this temporary StringBuffer
object and its subsequent conversion back into a String
object is very expensive.
In summary, the both lines of code above result in the creation of three objects:
- A
String
object at location 0
- A
StringBuffer
object at location 10
- A
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 DUP4 LDC #2 <string "Stanford" >6 Invokespecial #13 <method java.lang.StringBuffer (java.lang.String) >9 astore_1 Aload_1 LDC #1 <string "lost!!" > invokevirtual #15 <method java.lang.StringBuffer append (java.lang.String) > Pop
The bytecode at locations 0 to 9 are executed for the first line of code:
StringBuffer=newstringbuffer("Stanford");
The bytecode at location ten to and then executed for the concatenation:
Str. Append("lost!!" );
Notice that, as was the case in the first example, this code invokes the append
method of a StringBuffer
object. Unlike the first example, however, there is no need to create a temporary and then StringBuffer
convert it into a String
object. T His code creates is only one object, the StringBuffer
, at location 0.
In conclusion, StringBuffer
concatenation is significantly faster than String
concatenation. Obviously, StringBuffer
s should is used in this type of operation when possible. If String
the functionality of the class is desired, consider using a for concatenation and then StringBuffer
performing one conver Sion to String
.
Reggie Hutcherson is a Sun technology evangelist. He evangelizes Sun ' s Java 2 Platform technologies around the world concentrating on J2SE and the HotSpot performance Engin E.
Reproduced StringBuffer versus String