JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

Difference between StringBuffer, StringBuilder and String in Java




String vs StringBuffer in Java


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:

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 StringBuilder.java 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.

Difference between StringBuffer, StringBuilder and String in Java





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


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.

.