• Post Reply Bookmark Topic Watch Topic
  • New Topic

StringBuffer  RSS feed

 
Tmmet Johnson
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Could anyone please mail be which one would be better of the below two in
performance.
How many strings will be created in Method1 and how many in Method2?
Thanks in advance.

Method - 1

String b = "beta";
StringBuffer buf = new StringBuffer("The strings are ");
buf.append("a"+ b + "c");

Method - 2
String b = "beta";
StringBuffer buf = new StringBuffer("The strings are ");
buf.append("a").append(b).append("c");
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using StringBuffers, then saying buffer.append(a + b) is just silly. Multiple calls to append() are definitely better.
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For StringBuffer's you aren't actually creating Strings until you output the results as a String. The append statements themselves do not create new strings, they just append to an existing object. Think of them as linked lists, you don't create a new linked list when you append an element.

In your example, though, you have strings defined explicitly such as "a", "b" and "c", and these will create new strings. In short, buff.append("x") creates a string, but not because of the buffer, because of the way the input is defined.

As a further example, the following code should create only one string:

String x = "hello"; <--- creation of string
StringBuffer buff = new StringBuffer(x);
buff.append(x).append(x).append(x).append(x).append(x);

Where's this line of code would create many strings:

buff.append("x").append("x").append("x").append("x").append("x");


Side note: There is that whole issue of strings being immuteable objects and being placed in a shared buffer pool, yada yada, but that I think that's beyond the scope of your question.
 
Akshay Kiran
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to add that the '+' operator on strings (not class String, but just strings) uses StringBuffer.append to do the job until java 1.4 (in 1.5, it uses StringBuilder), so using + (concatentation) or append is probably just the same.

but
if you're having
String s= "this" + "is" +"a"+"string";
and
StringBuffer sb= new StringBuffer("this").append("is").append("a").append("string");
then the second would be more efficient (java 1.4) because the first converts everything to stringbuffer, does the same operations as in the second case and converts it back to a String.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Akshay Kiran:
the '+' operator on strings (not class String, but just strings)


There's absolutely no such distinction. A "quoted literal" is a java.lang.String.
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

String s= "this" + "is" +"a"+"string";
and
StringBuffer sb= new StringBuffer("this").append("is").append("a").append("string");
then the second would be more efficient (java 1.4) because the first converts everything to stringbuffer, does the same operations as in the second case and converts it back to a String.


I'm not so sure that its so much better in this example. Both implementations still create 4 strings specifically: "this", "is", "a" and "string".

Quick Analysis:

Memory Usage = 7 Strings:
"this"
"is"
"a"
"string"
"this is"
"this is a"
"this is a string"
Performance = 3 calls to String.concat()

Memory Usage = 5 Strings and 1 string buffer
"this"
"is"
"a"
"string"
"this is a string" (if you add sb.toString())
Performance = 3 calls to StringBuffer.append()

So as a whole, not much different, although if the words "this", "is", "a" and "string" were previously defined, than the difference is 1 string versus 3 strings, which is more significant.
 
Vlado Zajac
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Akshay Kiran:
I would like to add that the '+' operator on strings (not class String, but just strings) uses StringBuffer.append to do the job until java 1.4 (in 1.5, it uses StringBuilder), so using + (concatentation) or append is probably just the same.

but
if you're having
String s= "this" + "is" +"a"+"string";
and
StringBuffer sb= new StringBuffer("this").append("is").append("a").append("string");
then the second would be more efficient (java 1.4) because the first converts everything to stringbuffer, does the same operations as in the second case and converts it back to a String.


Yes, + does uses StringBuffer, so a+b+c+d is equivallent to
new StringBuffer(a).append(b).append(c).append(d).toString() and is equally efficient (in Java 1.5 replace StringBuffer with StringBuilder).

Because "this", "is", "a" and "string" are literals, they are compile time constants and so "this"+"is"+"a"+"string" is equivallent to "thisisastring" in terms of performance and it is more efficient than using StringBuffer.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Granted, it would be nice if locals were implicitly final, and explicitly variable, instead of the other way around. Maybe a language like this will exist some day.
 
Adam Richards
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're intersted in performance, you'll want to run some timers to confirm this, but I understand that if all the strings are known at compile time, it's just as fast to say

Reason: The compiler does a pretty good job optimizing.

If on the other hand, the strings aren't known until runtime, then yes, StringBuffer (or StringBuilder) is fast than String if you do many appends.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!