difference between String buffer and String builder?

There is no much difference between string buffer and string
builder.only thing is all the methods available in string
buffer class are  synchronized methods.and the method
available in string builder class are non-synchronized methods.
      If we want thread safety then we have to go for string
buffer ,but performance goes down.if we don't want thread
safety the we can go for string builder
 
..............................................................
 
Both are mutable ,that means we can change value of the
instance variable using append,insert and ...
But only one difference when you go for the multi-thread
concept you use StringBuffer,Because it is thread safe ,that
means, if one thread access this stringbuffer variable on
the time another thread cant access this stringbuffer,so
stringbuffer is slowest performance compare to stringbuilder. 
..............................................................
 
StringBuffer came first. Sun was concerned with correctness under all conditions, so they made it synchronized to make it thread-safe just in case.
StringBuilder came later. Most of the uses of StringBuffer were single-thread and unnecessarily paying the cost of the synchronization.
Since StringBuilder is a drop-in replacement for StringBuilder without the synchronization, there would not be differences between any examples.
Simply use StringBuilder unless you really are trying to share a buffer between threads.
If you are trying to share between threads, either use StringBuffer or consider whether higher-level synchronization is necessary, e.g. synchronizing the methods that use the buffer or synchronizing on the class that has the buffer as an instance var.
 
 

5 comments:

Aashish said...

String is an immutable class while StringBuilder & StringBuffer are mutable classes. StringBuffer is synchronized while StringBuilder is not.

Below link can be useful to find more differences between String, StringBuffer & StringBuilder

Difference between String, StringBuffer and StringBuilder

lingmaaki said...

More .....C# Interview Questions

Ling

Priya Kannan said...

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
Dot Net Training in Chennai

Mrbk30 said...

Very Informative blog thank you for sharing. Keep sharing.

Best software training institute in Chennai. Make your career development the best by learning software courses.

ios course in chennai
devops training in chennai
cloud computing courses in chennai

Block said...

I wish to show thanks to you just for bailing me out of this particular
trouble.As a result of checking through the net and meeting
techniques that were not productive, I thought my life was done.
mysql online course in Chennai
unix certification in Chennai
IT training institute in Chennai

Post a Comment