File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes using plus inside StringBuffer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "using plus inside StringBuffer" Watch "using plus inside StringBuffer" New topic

using plus inside StringBuffer

kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 323
HI all
Suppose i have 3 variables. If i write like this
StringBUffer sb = new StringBuffer("[");
Is this code creating new string objects because of using + inside append.


Failures are practice shoots for success.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Before the StringBuffer.append method is called the supplied argument has to be evaluated. In your case the supplied argument is "',"+variable1+",'" which when evaluated will cause new String objects to be created.

Why not do:

Note: In Java 1.5, if multithreading is not an issue, you can use StringBuilder in place of StringBuffer.

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
somkiat puisungnoen
Ranch Hand

Joined: Jul 04, 2003
Posts: 1312
Sure ...

The + operator to concatenate two String objects.

The + operator appears innocent, but the code generated produces some surprises. Using a StringBuffer for concatenation can in fact produce code that is significantly faster than using a String. To discover why this is the case, we must examine the generated bytecode from our two examples. The bytecode for the example using String looks like this:

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
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 at locations 0 through 9 is executed for the first line of code, namely:

String str = new String("Stanford ");

Then, the bytecode at location 10 through 29 is executed for the concatenation:

str += "Lost!!";

Things get interesting here. The bytecode generated for the concatenation creates a StringBuffer object, then invokes its append method: the temporary StringBuffer object is created at location 10, and its append method is called at location 23. Because the String class is immutable, a StringBuffer must be used for concatenation.

After the concatenation is performed on the StringBuffer object, it must be converted back into a String. This is done 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 are very expensive.

In summary, the two lines of code above result in the creation of three objects:

1. A String object at location 0
2. A StringBuffer object at location 10
3. A String object at location 26

Reference Website

Java Developer, Thailand
shai koren
Ranch Hand

Joined: Nov 04, 2001
Posts: 48

yes, this code will create new String objects and thus is bad news.

you better stick to this format:
StringBUffer sb = new StringBuffer("[");

snd so on

Shai koren<br />SCJP2 <br />SCEA (well yea only part 1 so far)
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8927


Your purpose of using StringBuffer is lost if you use "+".

kundan varma
Ranch Hand

Joined: Mar 08, 2004
Posts: 323
HI all
I agree. Here's the link:
subject: using plus inside StringBuffer
It's not a secret anymore!