Win a copy of Rust Web Development this week in the Other Languages 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

The Difference Between StringBuffer & StringBuilder

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi...
Ranchians
Please tell me about the difference between StringBuffer & StringBuilder.
I'm Really confused regarding these 2.
So help me in this...........
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know, usage wise both are same, I mean that both provide comparable methods.
The only difference is in performance context. StringBuffer is synchronized while stringbuilder is not.
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
StringBuffer is in older versions of the Java API and StringBuilder is in Java 5.0 and up. These two classes are almost identical and they are usually interchangeable.

StringBuffer is synchronized to handle sharing of the object by multiple threads. Modern thought is that this is not generally necessary and only slows things down. Generally, you will use StringBuilder if you are using Java 5.0 or later (also known as Java 1.5).
[ March 15, 2008: Message edited by: Kaydell Leavitt ]
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Both StringBuffer and StringBuilder are identical.
Main difference is....
StringBuffer is Thread safe, so, it comes with Performance issue.
while StringBuilder is not thread safe.
 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
identical in the following sense...

I'll just add that if you done like I had and written a large program in a single thread environment, with all kinds of StringBuffers, and you wanted to switch to faster StringBuilders, just replace the word StringBuffer with the word StringBuilder wherever it occurs, and all else will be the same.
 
Sheriff
Posts: 22563
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suresh, was it necessary to kick a year old thread with an answer that Kaydell already gave?
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Almost both will be identical,but StringBuilder is unsynchronized whereas StringBuffer is synchronized

so StringBuilder is not thread safe whereas StringBuffer is thread safe.

so usage of StringBuilder is efficient in single thread.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic