Difference between String and StringBuffer in Java

String vs StringBuffer in Java
String and StringBuffer are two classes which is most widely used in any Java program. If I say you can not write a program without using String in Java than it would not be exaggeration. String is everywhere, main method accept 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 its 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 gone 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

Difference between String and StringBuffer in JavaHere 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) 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 new object.

2) If you are using  + operator for concatenating multiple String than you should not be worried much because  based upon Java implementation call to + 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 an 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 does 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. Most important difference to remember between String and StringBuffer is mutability.

In 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 more clear, concise and gives 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

No comments:

Post a Comment