• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Use of StringBuffer instead of String

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there
For my method that builds the criteria string from the search criteria I currently use the append() method of StringBuffer and then call toString() on the buffer at the end. Originally I used the + operator and the += operator with the String class but I was concerned that under the bonnet each time I called += for example in the following:

my application was then having to allocate a new String, copy the existing string plus the extra string into it then again and again as I added more criteria. So it seemed a waste of resources.
Is it okay to append the criteria to a StringBuffer instead as this is what it all gets translated to in any case. It is more long winded but its easy to read.
Any opinions welcome. Thanks alot
Sam
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're using the append() method in a loop construct I think that's good choice. If the += syntax is present in a loop it will/could have an impact on the performace of the application. It may even result in points being deducted, but I'm not certain that's the case. The graders seem to nit-pick some of the finer details, so I think a StringBuffer is the way to go.
Also, the JDK states that:

String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved...
As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger.

. Thread-safe and no buffer overflow = GOOD CHOICE.
[ May 06, 2003: Message edited by: Christian Garcia ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean this sort of thing:
?
This is possibly the other extreme with points lost for lack of readability.
[ May 06, 2003: Message edited by: Barry Gaunt ]
[ May 06, 2003: Message edited by: Barry Gaunt ]
 
Samantha O'Neill
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually Barry I have something more like this:

As its very easy to read. My concern is that its too long-winded. But on the other hand using the + operator would mean allocating a new String each time I use +=. For example

I'm not concerned about making sure my buffer is synchronized as this processing is on the client side so there are no other clients or other threads that could corrupt its contents in any place.
I agree Barry that the .append().append() idiom is ugly and difficult to read so have most gone for the String and + method?
Thanks for the input
Sam
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sam,
Personally I think your first example is much easier to read, and it has the advantage of not requiring new Strings to bounce in and out of scope. That is the one I would go for.
My code also uses StringBuffers for creating the search string.
Regards, Andrew
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic