Difference between String and StringBuffer/StringBuilder in Java

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!

386 comments

  1. monika bulchandani

    thanks

  2. ultimate explaination.. thanks

  3. thankyou very much.my all doubts are cleared now.very well explained in simple language.

  4. well explained

  5. Good and clear explanation

  6. Superb explanation :)

  7. NarayananKannan

    Very Useful, Thanks
    Please keep on Updating like this ;)

  8. NarayananKannan

    Very Useful, thanks

  9. i still have some issues with the synchronization thing ( the diference between the two types )

  10. but why StringBuffer n StringBuilder class mutable ????????

  11. Clear and concise post explaining the 3 important classes. Thanks.

  12. Very good explanation….

  13. Real time example for synchronization.

  14. Md. abdul kaium

    super explanation .

  15. very good description

  16. Explanation is superb can u please give one example by using all those in single program

  17. easily understood… :)
    thanks for sharing this

  18. very well explained in simple language……..

  19. Thank you so much

  20. Thank you so much ..!!! this helped me a lot.

  21. Fantastic goods from you, man. I’ve understand your stuff previous to and you are just too excellent.

  22. Thanks a million. Your explanations are very good. May you be more and more successful.

  23. Very useful explanation ..I have one question .How do you put lock on objects.?

  24. well structured explanation. Thnx

  25. Nice….It’s good but it need’s more explanation about synchronization….

  26. nice explanation..but if u write small prgm den more clarity vl come

  27. thanks

  28. very useful

  29. Nice explanation. and thanks too…..
    But my question is why they made String is immutable and why they made StringBuffer and StringBuilder as mutable?

  30. Great work !!

  31. nice, now I got clear view about String and StringBuilder/StringBuffer

  32. wow!,nice explanation….

  33. very good explanation..thanks..

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>