• 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

When to use StringBuffer/StringBuilder in stead of Strings

 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe just a question. Why should you use StringBuffer of StringBuilder if you could just use String? Outweighs the overhead of creating a new class the use of different strings, which should be garbage collected at the end.

I'm just curious, because until now I mostly use Strings as of a habit in stead of the StringBuffer/StringBuilder class while man
 
Remko Strating
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've read the following article which states that by repeated updates of the String a stringbuffer/stringbuilder could be more efficient.

String Concatenation/Performance
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that's basically it. If you are doing a lot of text manipulations, a StringBuilder is faster. However, this will only translate into measurable performance gains if there are many thousands of updates and/or the Strings are very long.

StringBuilder is better than StringBuffer for most applications, because StringBuffer has unwanted synchronisation.
 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use the string concatenation operator (+) to combine a few strings into one.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Manuel Leiria:
You can use the string concatenation operator (+) to combine a few strings into one.



Yes, of course you can, but doing so is less efficient than using StringBuilder, if the number of strings to combine is more than a very few.

Good programmers sometimes quite reasonably decide to use concatenation operator even when StringBuilder would be more efficient. They may judge that the conciseness and clarity of concatenation operator is worth a small efficiency loss. Only if the number of concatenations is very large or the strings are very long, will the improved efficiency of StringBuilder make a big performance difference.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic