This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

StringBuffer question

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I've always had a nagging question about StringBuffers. Is the following initialization an okay thing to do? Or am I creating a bunch of unneccessary string objects -- where a and b are runtime variables, like request parameters?
String a = "the";
String b = "a";
StringBuffer s = new StringBuffer(a + " dog ate " + b + " bone.");
Or would I be better off doing:
StringBuffer s = new StringBuffer(25);
s.append(a);
s.append(" dog ate ");
s.append(b);
s.append(" bone.");
Or does it matter? I tend to use the first when building StringBuffers from variables, but am afraid I am creating a lot of extra strings...
Thanks,
--jeff
 
Ranch Hand
Posts: 1512
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not really sure on this one. It may not really matter though, because I know when you concatenate/create strings using the "+" sign, it actually uses a StringBuffer object in the background to get it's job done.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
By no means do I pretend to be an authority on efficiency. In that light, here are my two cents...
From the API Specification for StringBuffer:


String buffers are used by the compiler to implement the binary string concatenation operator +. For example, the code:
x = "a" + 4 + "c"
is compiled to the equivalent of:
x = new StringBuffer().append("a").append(4).append("c").toString()


Looking further at the documentation, StringBuffer.append(Object - or String) creates a new StringBuffer object.
So, it would seem to me, that looking at the first style:
StringBuffer s = new StringBuffer(a + " dog ate " + b + " bone.");
a + " dog ate " + b + " bone." creates one new String object from this:
new StringBuffer.append(a).append(" dog ate ").append(b).append(" bone.")
while creating 5 new StringBuffer objects (one from the implicit call to new StringBuffer plus one for each call to append) plus one more for your explicit call to new StringBuffer(String) - bringing the total to 6 StringBuffer objects.
Now, your second style:
StringBuffer s = new StringBuffer(25);
s.append(a);
s.append(" dog ate ");
s.append(b);
s.append(" bone.");
would seem to create first one StringBuffer from new StringBuffer(int) and then one more for each call to append - a total of 5 StringBuffer objects and no "extra" String objects.
Of course, I don't know what I'm talking about.
Good Luck,
-Dirk Schreckmann
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your second implementation would be the best route. Later on, when you use strings *cough* JavaMail *cough* you'll be happy you know StringBuffer rather well. From my understanding, it's the most efficent way to create and include multiple strings.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic