Results 1 to 10 of 10
Thread: StringBuilder v/s StringBuffer
- 04-11-2008, 08:11 AM #1
Member
- Join Date
- Apr 2008
- Posts
- 13
- Rep Power
- 0
StringBuilder v/s StringBuffer
Hi All,
I want to know the difference between StringBuilder & StringBuffer classes. I know that StringBuffer is thread safe and StringBuilder is not, but any practical examples and code would be appreciated. Also, if we mark the methods written for StringBuilder objects as 'synchronized', would they become thread safe? please respond soon, its urgent.
Thanks In Advance!:):)
- 04-11-2008, 08:18 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Hi Pooja,
Welcome to our community.
Actually both are quite same, except StringBuilder is not synchronized as StringBuffer. So if you use multiple threads at a time, you are in real trouble.
- 04-11-2008, 08:32 AM #3
Member
- Join Date
- Apr 2008
- Posts
- 91
- Rep Power
- 0
So,
Eranga How We Will Decide When To Use Which?
- 04-11-2008, 08:41 AM #4
Hello Pooja,
if we mark the methods written for StringBuilder objects as 'synchronized', would they become thread safe?
No Only the method calling will be thread safe.
sanjeev
- 04-11-2008, 08:47 AM #5
Member
- Join Date
- Apr 2008
- Posts
- 13
- Rep Power
- 0
StringBuilder v/s StringBuffer
Hi All,
OK , the doubt regarding synchronized methods is clear now. thank you Sanjeev.
But back to my main question, please share sample code to show the difference in behavior of these two classes, when multiple threads are involved.
- 04-11-2008, 09:29 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
No you can't involve with multiple thread in StringBuilder.
In most of the cases I prefer StringBuffer. Because in real world application we have to deal with large number of threads. I check this in th past using JUnit test. I don't have a sample code for this now. If you can just write a simple application and do a unit test. You can see it your self.
- 04-11-2008, 09:33 AM #7
Member
- Join Date
- Apr 2008
- Posts
- 13
- Rep Power
- 0
Thank you very much Eranga.
I will try a small application myself and get back if i face any problems!:):)
- 04-11-2008, 09:34 AM #8
Member
- Join Date
- Apr 2008
- Posts
- 91
- Rep Power
- 0
Hei In Our Application, We Are Told To Migrate The StringBuffer With StringBuilder. Can you give Some of your views on it?
- 04-11-2008, 09:37 AM #9
Member
- Join Date
- Apr 2008
- Posts
- 13
- Rep Power
- 0
Hi,
probably for increase in performance thay might have asked you to migrate.
because StringBuilder is very efficient in case of single thread.
and migration wont take much effort, because almost all the methods of these two classes are same.
- 04-11-2008, 09:38 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
Again all those things comes to the picture in real world, because of multi threading. In single thread application, which is the most common situation we comes up with, if you can avoid the overhead of synchronization the StringBuilder makes little faster actually.
Similar Threads
-
StringBuffer situation
By orchid in forum New To JavaReplies: 6Last Post: 08-12-2008, 01:39 PM -
Difference between StringBuilder & StringBuffer
By Pooja Deshpande in forum New To JavaReplies: 5Last Post: 04-16-2008, 12:51 PM -
StringBuffer
By Java Tip in forum Java TipReplies: 0Last Post: 11-08-2007, 08:33 AM -
Help with StringBuffer
By Marcus in forum AWT / SwingReplies: 2Last Post: 07-04-2007, 05:50 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks