Easy tech stuff!

Difference between String and StringBuffer/StringBuilder in Java

Posted: February 15th, 2008 | Author: Nischal Shetty | Filed under: Java | 19 Comments »

Well, the most important difference between String and StringBuffer/StringBuilder in java is that String object is immutable whereas StringBuffer/StringBuilder objects are mutable.

By immutable, we mean that the value stored in the String object cannot be changed. Then the next question that comes to our mind is “If String is immutable then how am I able to change the contents of the object whenever I wish to?” . Well, to be precise it’s not the same String object that reflects the changes you do. Internally a new String object is created to do the changes.

So suppose you declare a String object:

String myString = “Hello”;

Next, you want to append “Guest” to the same String. What do you do?

myString = myString + ” Guest”;

When you print the contents of myString the output will be “Hello Guest”. Although we made use of the same object(myString), internally a new object was created in the process. So, if you were to do some string operation involving an append or trim or some other method call to modify your string object, you would really be creating those many new objects of class String.

Now isn’t that a performance issue?

Yes, it definitely is.

Then how do you make your string operations efficient?

By using StringBuffer or StringBuilder.

How would that help?

Well, since StringBuffer/StringBuilder objects are mutable, we can make changes to the value stored in the object. What this effectively means is that string operations such as append would be more efficient if performed using StringBuffer/StringBuilder objects than String objects.

Finally, whats the difference between StringBuffer and StringBuilder?

StringBuffer and StringBuilder have the same methods with one difference and that’s of synchronization. StringBuffer is synchronized( which means it is thread safe and hence you can use it when you implement threads for your methods) whereas StringBuilder is not synchronized( which implies it isn’t thread safe).

So, if you aren’t going to use threading then use the StringBuilder class as it’ll be more efficient than StringBuffer due to the absence of synchronization.

Incase you do not know – Here’s how you use StringBuilder

A simple Example to demonstrate that String object is Immutable

Incase you still have any doubts regarding String or StringBuilder then do leave a comment. I’ll be more than eager to help you out.

Note: StringBuilder was introduced in Java 1.5 (so if you happen to use versions 1.4 or below you’ll have to use StringBuffer)

Link: Have doubts about the ‘final’ keyword? check out my post – Click Here!

http://www.techtamasha.com/wp-content/plugins/sociofluid/images/digg_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/reddit_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/delicious_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/furl_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/technorati_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/google_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/myspace_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/facebook_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/twitter_48.png

19 Comments on “Difference between String and StringBuffer/StringBuilder in Java”

  1. 1 Alaeddine said at 4:46 pm on May 8th, 2008:

    A very good unswer. Thnks a lot.

  2. 2 Abhishek said at 7:07 am on July 24th, 2008:

    Please furnish a snippet of StringBuffer also.

  3. 3 saleem said at 10:18 am on July 21st, 2009:

    Excellent Answer. please give Explanation for StringBuffer also. thanks.

  4. 4 saleem said at 10:19 am on July 21st, 2009:

    Excellent Answer. please give Explanation for StringBuffer also. thanks.

  5. 5 sampath said at 6:07 am on October 30th, 2009:

    you told StringBuilder is not synchronized(i.e) not threadsafe.But if i declare StringBuilder Object inside a method,whether it will be threadsafe object or not.
    Also,StringBuffer is synchronized,and if i declare StringBuffer object outside a method as instance/class level,can we call it as thread-safe variable?

  6. 6 Nischal Shetty said at 6:19 pm on November 3rd, 2009:

    @sampath

    A variable declared inside a method should be thread safe since method variables are allocated space in the stack which is specific to every thread.

    In the case of StringBuffer being declared as an instance variable, it should behave in a thread safe manner. But dont take my words blindly. Research a bit more and do correct me if I’m wrong.

  7. 7 Hari said at 6:22 pm on December 15th, 2009:

    no need to search any site for this answer this is more than enough………its 200% is good.

  8. 8 Gezahegn said at 4:22 pm on December 24th, 2009:

    Waw! It is a fantastic and logical explanation. But i need more about StringBuffer With example demonstrated.

    Tnx!!!!

  9. 9 Nischal Shetty said at 7:25 pm on December 24th, 2009:

    @Gezahegn

    You can check out a tiny example on StringBuilder… StringBuffer would be the same as well…

    http://www.techtamasha.com/heres-how-you-use-stringbuilder/30

  10. 10 Yeshwanth said at 11:53 am on January 19th, 2010:

    Please provide some more difference’s so that these were helpful for interview purposes.

    Thx.,

  11. 11 Ashish Vyas said at 1:54 pm on February 14th, 2010:

    Wow……

    Excellent way of explaining. I am quite impressed by the way of presentation.

    Its like….”Read once and Remember forever !”

    Grt job buddy.

    Kudos to TechTamasha team.

  12. 12 Jyotsna Gupta said at 11:24 am on February 22nd, 2010:

    thanks Nischal Shetty….

    I cleared my all doubt about String and StringBuffer

  13. 13 AJ said at 6:54 pm on February 27th, 2010:

    What an explanation …hats off… Please keep up this good work.

    thank you very much

  14. 14 Shriniket said at 3:52 pm on March 6th, 2010:

    Excellent Explanation. With simple and smart example.

  15. 15 manas said at 10:43 am on March 29th, 2010:

    Excellent …I was very much confused before that,,,but everything is been cleared…

  16. 16 Thaks said at 4:58 am on April 14th, 2010:

    very useful content

  17. 17 Ashok said at 2:52 am on April 24th, 2010:

    Superb Answer with excellent example……I like it…. I got to know the difference bt String and StringBuffer now exactly….

  18. 18 amm said at 1:23 pm on May 28th, 2010:

    well thats a great explanation , i’ve been all around and the most precise post i’ve come through , now i understood it perfectly , those phone screening interviewers always ask this question , thanks a lot u r fantastic , and if u don’t mind i’d come back to you for some other doubts ,,

  19. 19 Nischal Shetty said at 3:53 pm on May 28th, 2010:

    @amm glad to know it helped you


Leave a Reply