Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The Difference Between StringBuffer & StringBuilder

 
Dattatraya Baravkar
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • 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...........
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • 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.
 
Kaydell Leavitt
Ranch Hand
Posts: 689
Eclipse IDE Firefox Browser Mac OS X
  • Mark post as helpful
  • send pies
  • 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 ]
 
Suresh Pritmani
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • 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.
 
Fred Hamilton
Ranch Hand
Posts: 684
  • Mark post as helpful
  • send pies
  • 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.
 
Rob Spoor
Sheriff
Pie
Posts: 20511
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suresh, was it necessary to kick a year old thread with an answer that Kaydell already gave?
 
Birla Murugesan
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic