aspose file tools*
The moose likes Beginning Java and the fly likes The Difference Between StringBuffer & StringBuilder Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The Difference Between StringBuffer & StringBuilder" Watch "The Difference Between StringBuffer & StringBuilder" New topic
Author

The Difference Between StringBuffer & StringBuilder

Dattatraya Baravkar
Greenhorn

Joined: Jan 24, 2008
Posts: 10
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

Joined: May 18, 2007
Posts: 341

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

Joined: Nov 18, 2006
Posts: 689

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

Joined: May 24, 2009
Posts: 8
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

Joined: May 13, 2009
Posts: 679
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

Joined: Oct 27, 2005
Posts: 19722
    
  20

Suresh, was it necessary to kick a year old thread with an answer that Kaydell already gave?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Birla Murugesan
Ranch Hand

Joined: Nov 25, 2008
Posts: 66

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: The Difference Between StringBuffer & StringBuilder