Difference between String and StringBuffer in Java

String vs StringBuffer in Java
String and StringBuffer are two classes which 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 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 e.g. 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 difference between String and StringBuffer, which makes StringBuffer 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 very popular Java interview question.

Difference between String and StringBuffer in Java

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

1) 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 new String object while modifying StringBuffer object won't create a new object.

2) If you are using  + 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 StringBuider based upon JVM implements Java 1.5 or lower version.

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

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

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

That's all on Difference between String and StringBuffer in Java. The most important difference to remember between String and StringBuffer is mutability.

Difference between String and StringBuffer in JavaIn Summary, you 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. On last note, Consider using StringBuilder in Java 5 onwards.

Other Java tutorials from Java67 Blog:
Difference between wait and sleep in Java
Difference between ArrayList and HashMap in Java
What is class file in Java 
Difference between TreeSet and TreeMap in Java
What is Serialization in Java

1 comment: