|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

04-11-2008, 10:11 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 12
|
|
|
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, 10:18 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-11-2008, 10:32 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 91
|
|
|
So,
Eranga How We Will Decide When To Use Which?
|
|

04-11-2008, 10:41 AM
|
 |
Senior Member
|
|
Join Date: Apr 2008
Location: Delhi(India)
Posts: 251
|
|
|
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, 10:47 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 12
|
|
|
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, 11:29 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
Originally Posted by Pooja Deshpande
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.
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

04-11-2008, 11:33 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 12
|
|
Thank you very much Eranga.
I will try a small application myself and get back if i face any problems!  
|
|

04-11-2008, 11:34 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 91
|
|
|
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, 11:37 AM
|
|
Member
|
|
Join Date: Apr 2008
Posts: 12
|
|
|
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, 11:38 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
|
|
|
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|