String and StringBuffer both are the classes which operate on strings. The object of String class is of fixed length.
The object of the StringBuffer class is growable. The basic difference between String and StringBuffer is that the object of the “String” class is immutable.
The object of the class “StringBuffer” mutable.
What is meant by immutable:
we cannot append,remove new string to created object.StringBuffer objects provide more functionality to the strings as compared to the class String.
Hence, it is preferable to work with StringBuffer instead of class String.so when we want to make fixed size string use string object and when we want perform operation like append remove we use stringBuffer class object.
String Object:
Creating empty string object → String str = new String (“Hello”);
Appending new string → str.concat(“World !”);
Printing string object → System.out.println(str);
Output → Hello
StringBuffer Object:
Creating empty string object → String str = new String (“Hello”);
Appending new string → str.append(“ World !”);
Printing string object → System.out.println(str);
Output → Hello World !
-
Concat operation on StringBuffer is O(1 ) while for String Class it is O(n).
-
Because String is immutable hence new object is created for every concat.