String and StringBuffer both are the classes which operate on strings. StringBuffer class is the peer class of the class String. 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.
Content: String Vs StringBuffer
|Basis for Comparison||String||StringBuffer|
|Basic||The length of the String object is fixed.||The length of the StringBuffer can be increased.|
|Modification||String object is immutable.||StringBuffer object is mutable.|
|Performance||It is slower during concatenation.||It is faster during concatenation.|
|Memory||Consumes more memory.||Consumes less memory.|
|Storage||String constant pool.||Heap Memory.|
Definition of String
“String” is a class in Java. The object of the class String is of fixed length, and most important to remember, the object of String class is “immutable”. Once you initialize the String object, you can not modify that object again. The object of the class String is stored in String constant pool.
Let us first understand that, whenever you create any string; you create an object of type string. The string constants are also string objects.
System.out.println("Hello this is Teckpix Solution");
In above statement, the string “Hello this is Teckpix Solution” is a String constant.
Now let us understand the mutability of the String object with the help of an example.
String str=new String ("Teckpix"); str.concat("Solution"); system.out.println( str ); //output Teckpix
In above code, I tried to concatenate the two strings “Teckpix” and “Solution”. As we know whenever a string is created that means the object of type String is created. Hence, the string “Teckpix” creates an object, whose reference is assigned to the string object “str”. Next, I tried to concatenate an another string “Solution” with the string “Teckpix” using the method “concat( )” of the class String.
As the String objects are immutable, no modification is done to the string “Teckpix” and the new string “Solution” creates an another object in the string constant pool. But, the reference to the object “Solution” is not caught by any object hence the reference to object Solution is lost though it is still present in the string constant pool. As no modification is done to the object Teckpix, when I print the object str to which I assigned the reference of Teckpix earlier, will print the string “Teckpix” only.
Definition of StringBuffer
The class “StringBuffer” is the peer class of the class “String”. The class StringBuffer provides more functionality to the strings. The object of the StringBuffer class is mutable that is its object can be modified. The length of the StringBuffer object is growable. You can insert the characters or substrings in the middle of string literal that is assigned to the StringBuffer object or at the end of it. StringBuffer allocates space for 16 additional characters when no specific length is requested.
Let us understand the mutability of the StringBuffer object with the help of an example:
StringBuffre Sb= new StringBuffer ("Teckpix"); Sb.append("Solution"); system.out.println( Sb ); // Output Teckpix Solution
As we know that the StringBuffer object is mutable. The method append( ) modifies the StringBuffer object Sb to which initially, the reference of the object”Teckpix” is assigned earlier. The method append( ) appends the new string literal “Solution”, to the end of the string literal “Teckpix” . Now when I print the object Sb it will print the modified string object “Teckpix Solutions”.
Key Difference Between String and StringBuffer
- The length of String object is fixed but the length of an object of StringBuffer can be increased when required.
- String object is immutable i.e. it’s object can’t be reassigned again whereas, the object of StringBuffer is mutable.
- String object is slower in performance whereas, the StringBuffer object is faster.
- String object consumes more memory whereas, StringBuffer objects consumes less memory.
- String objects are stored in a constant pool whereas, StringBuffer objects are stored on heap memory.
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.