Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using plus inside StringBuffer

 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI all
Suppose i have 3 variables. If i write like this
StringBUffer sb = new StringBuffer("[");
sb.append("',"+variable1+",'");
sb.append("',"+variable2+",'");
sb.append("',"+variable3+",'");
Is this code creating new string objects because of using + inside append.

THanks
kundan
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
somkiat puisungnoen
Ranch Hand
Posts: 1312
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
http://www.javaworld.com/javaworld/jw-03-2000/jw-0324-javaperf.html
 
shai koren
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kundan

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

you better stick to this format:
StringBUffer sb = new StringBuffer("[");
sb.append("',").append(variable1).append(",'");
sb.append("',").append(variable2).append(",'");


snd so on
 
Pradeep bhatt
Ranch Hand
Posts: 8927
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kundan

Your purpose of using StringBuffer is lost if you use "+".
 
kundan varma
Ranch Hand
Posts: 323
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI all
THanks
kundan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic