Difference between String and StringBuffer in Java? Answer

String vs StringBuffer in Java
String and StringBuffer are two classes that are most widely used in any Java program. If I say you can not write a program without using String in Java then it would not be an exaggeration. The string is everywhere, the main method accepts a String argument, logs are String, etc. Though many Java programmers familiar with String, not many are careful while performing operations on String, Since String is final in Java; every operation like converting String into Uppercase, creating SubString,  converting String to Lowercase all result in a separate new String Object, which can take trigger frequent garbage collection and affect your application performance.

Here comes StringBuffer in Java which is a mutable version of String, though it's not as feature-rich as String and you can not use StringBuffer in place of String but StringBuffer should be used whenever you are performing String concatenation instead of String in Java.

In this Java tutorial, we will see some differences between String and StringBuffer, which makes StringBuffer an ideal choice for performing String operations. By the way, if you went through any Java interview then you must be familiar with this question, String vs StringBuffer is a very popular Java interview question.

Difference between String and StringBuffer in Java

Here is a list of differences between StringBuffer and String, this is not a huge list, and barring significant differences between both of them you don't need to worry about remembering all those differences.


The first and most significant difference between String and StringBuffer in Java is that String is immutable in Java while StringBuffer is mutable. What this means is, performing any operation on String will create a new String object while modifying the StringBuffer object won't create a new object.

Difference between String and StringBuffer in Java

2. String Concatenation

If you are using the + operator for concatenating multiple String then you should not be worried much because based upon Java implementation call to the + operator is replaced with either StringBuffer or StringBuilder based upon JVM implements Java 1.5 or lower version.

3. Append

The StringBuffer.append()method is used to perform String concatenation in Java.

4. Conversion

Creating StringBuffer from String is easy, as StringBuffer accepts a String input. Similarly converting StringBuffer to String is also easy by using the toString() method in Java.

5. Type Hierarchy

Another significant difference between String and StringBuffer is that StringBuffer and String do not share the same type hierarchy, which means you can not cast String to StringBuffer in Java. any such attempt will result in ClassCastException in Java.

That's all on the difference between String and StringBuffer in Java. The most important difference to remember between String and StringBuffer is mutability. I mean, String is Immutable in Java and StringBuffer is a mutable object. 

This means any modification or operation on the String object will create a new String but you can modify the same StringBuffer object by appending or deleting characters, or replacing existing characters. 

Also,  should not create too much String garbage if you feel you need to modify a particular String more than often than create a StringBuffer instance and use that. Using + operator for string concatenation is also a good idea as it's clearer, concise, and gives the same performance as using StringBuffer in Java. 

P. S. - One last note, Consider using StringBuilder in Java 5 onwards. It's a close cousin of StringBuffer, in fact, is the same code just without the synchronization overhead. I mean, the method of StringBuffer is synchronized but the methods of StringBuilder are not synchronized. 

Over to you now, if you append a string into another String using StringBuilder, a new String object is created? True or False?

1 comment:

Feel free to comment, ask questions if you have any doubt.