Monday, 17 October 2016

5 Difference between StringBuffer, StringBuilder and String in Java

Though all three classes StringBuffer, StringBuilder and String are used for representing text data in Java there are some significant differences between them. One of the most notable differences between StringBuilder, StringBuffer, and String in Java is that both StringBuffer and StrinBuilder are Mutable class but String is Immutable in Java. What this means is, you can add, remove or replace characters from StringBuffer and StringBuilder object but any change on String object e.g. converting uppercase to lowercase or appending a new character using String concatenation will always result in a new String object. Another key difference between them is that both StringBuffer and String are thread-safe but StringBuilder is not thread-safe in Java. String achieves its thread-safety from Immutability but StringBuffer achieves it via synchronization, which is also the main difference between the StringBuffer and StringBuilder in Java.

String vs StringBuffer in Java

One of the oldest question from Java interviews are What is the difference between String and StringBuffer, it's been asked from ages, I guess started as soon as Java was released in the 90s. Let's first answer this question and then we will take a look at rather new StringBuilder vs String and StringBuffer vs StringBuilder questions.

1) First and foremost difference between String and StringBuffer is that former is Immutable while later is mutable. This means if you need to manipulate String data then wrap it inside a StringBuffer to avoid creating lots of small and temporary String objects and putting pressure on Garbage collector.

2) Even though both StringBuffer and String are thread-safe, String achieves its thread-safety by using Immutability while StringBuffer achieves it by synchronizing methods which change the state e.g. append(), delete() etc.

By the way, apart from above differences, both String and StringBuffer also has some key similarities e.g. both represent text data, both are defined in java.lang package, both classes exists in JDK from the first release and both are thread-safe in Java.

Here is also a nice diagram to highlight the mutable and immutable difference between String and StringBuffer in Java:

5 Difference between StringBuffer, StringBuilder and String in Java

StringBuffer vs StringBuilder in Java

One of the follow-up questions of the difference between String and StringBuffer class is the difference between StringBuilder vs StringBuffer. That's why it's not enough to know only about StringBuffer and String, you must also know about its close cousin StringBuilder which was added as a drop-in replacement of StringBuffer in Java 1.5 release.

If you look at the code of class in JDK, you will realize that it's just StringBuffer without synchronized keyword, even comments and description of methods are same. Anyway, let's see some quick difference between StringBuffer and StringBuilder class in Java:

1) The most important difference between StringBuffer and StringBuilder is that former is thread-safe and later is not. Why? because all key methods of StringBuffer are synchronized, which was later removed while creating StringBuilder class.

2) Due to above property, their performance is also different. StringBuilder is much faster than StringBuffer. Since they are mostly used as local variables to manipulate String and not really shared between multiple threads, it's better to use StringBuilder in place of, StringBuffer.

Btw, apart from above differences they are also quite similar e.g. both StringBuffer and StringBuilder represent mutable String and both allow you to append and delete characters. If you want to learn more difference between them, please read Java: A Beginner's Guide by Herbert Schildt, one of the good books to learn Java fundamentals like this.

When to use the String, StringBuffer, and StringBuilder in Java

Now that you know the difference between these three similar classes, its time to use that knowledge and make a decision about when to use each of them.

1) Use String if you need text data which is relatively constant e.g. names, config parameters etc.

2) Use StringBuilder if are doing lots of String concatenation e.g. you are generating dynamic String by using programming logic.

3) Use StringBuffer when your String manipulation code is likely to be executed by multiple threads and you are sharing the instance of same StringBuffer. It's highly unlikely and if you are doing some just stop doing it and use StringBuiler in place.

That's all about the difference between String, StringBuilder, and StringBuffer in Java. Just remember that String is immutable in Java which means any modification in String will alway return in new String e.g. converting into upper or lower case or getting substring out of it. Key difference between String and StringBuffer is mutability. So, if you want to concatenate multiple String it's better to use StringBuffer than String.

Similarly, the key difference between StringBuilder and StringBuffer  is thread-safety and speed, since StirngBuffer is thread-safe it's slower than StringBuilder. This is also one of the best coding practice in Java, if you want to learn to see Java Coding Guidelines: 75 Recommendations for Reliable and Secure Programs, one of the must read for both intermediate and experienced Java developers.