Results 1 to 10 of 10
  1. #1
    Join Date
    Apr 2008
    Posts
    13
    Rep Power
    0

    Default 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!:):)

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  3. #3
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    So,
    Eranga How We Will Decide When To Use Which?

  4. #4
    sanjeevtarar's Avatar
    sanjeevtarar is offline Senior Member
    Join Date
    Apr 2008
    Location
    Jaipur(India)
    Posts
    318
    Rep Power
    7

    Default

    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

  5. #5
    Join Date
    Apr 2008
    Posts
    13
    Rep Power
    0

    Default 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.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  7. #7
    Join Date
    Apr 2008
    Posts
    13
    Rep Power
    0

    Smile

    Thank you very much Eranga.

    I will try a small application myself and get back if i face any problems!:):)

  8. #8
    javarishi is offline Member
    Join Date
    Apr 2008
    Posts
    91
    Rep Power
    0

    Default

    Hei In Our Application, We Are Told To Migrate The StringBuffer With StringBuilder. Can you give Some of your views on it?

  9. #9
    Join Date
    Apr 2008
    Posts
    13
    Rep Power
    0

    Default

    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.

  10. #10
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    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

  1. StringBuffer situation
    By orchid in forum New To Java
    Replies: 6
    Last Post: 08-12-2008, 02:39 PM
  2. Difference between StringBuilder & StringBuffer
    By Pooja Deshpande in forum New To Java
    Replies: 5
    Last Post: 04-16-2008, 01:51 PM
  3. StringBuffer
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-08-2007, 09:33 AM
  4. Help with StringBuffer
    By Marcus in forum AWT / Swing
    Replies: 2
    Last Post: 07-04-2007, 06:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •