Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-11-2008, 10:11 AM
Member
 
Join Date: Apr 2008
Posts: 12
Pooja Deshpande is on a distinguished road
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!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-11-2008, 10:18 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-11-2008, 10:32 AM
Member
 
Join Date: Apr 2008
Posts: 91
javarishi is on a distinguished road
So,
Eranga How We Will Decide When To Use Which?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-11-2008, 10:41 AM
sanjeevtarar's Avatar
Senior Member
 
Join Date: Apr 2008
Location: Delhi(India)
Posts: 251
sanjeevtarar is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-11-2008, 10:47 AM
Member
 
Join Date: Apr 2008
Posts: 12
Pooja Deshpande is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-11-2008, 11:29 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Quote:
Originally Posted by Pooja Deshpande View Post
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 04-11-2008, 11:33 AM
Member
 
Join Date: Apr 2008
Posts: 12
Pooja Deshpande is on a distinguished road
Thank you very much Eranga.

I will try a small application myself and get back if i face any problems!
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 04-11-2008, 11:34 AM
Member
 
Join Date: Apr 2008
Posts: 91
javarishi is on a distinguished road
Hei In Our Application, We Are Told To Migrate The StringBuffer With StringBuilder. Can you give Some of your views on it?
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 04-11-2008, 11:37 AM
Member
 
Join Date: Apr 2008
Posts: 12
Pooja Deshpande is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 04-11-2008, 11:38 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,609
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
StringBuffer situation orchid New To Java 6 08-12-2008 03:39 PM
Difference between StringBuilder & StringBuffer Pooja Deshpande New To Java 5 04-16-2008 02:51 PM
StringBuffer Java Tip Java Tips 0 11-08-2007 10:33 AM
Help with StringBuffer Marcus AWT / Swing 2 07-04-2007 07:50 AM


All times are GMT +3. The time now is 10:57 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org