• Networking
  • Programming
  • DBMS
  • Operating System
  • Internet
  • Hardware
  • Software

Tech Differences

Know the Technical Differences

Difference Between String and StringBuffer Class in Java

String-Vs-StringBufferString 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

  1. Comparison Chart
  2. Definition
  3. Key Differences
  4. Conclusion

Comparison Chart

Basis for ComparisonStringStringBuffer
BasicThe length of the String object is fixed.The length of the StringBuffer can be increased.
ModificationString object is immutable.StringBuffer object is mutable.
PerformanceIt is slower during concatenation.It is faster during concatenation.
MemoryConsumes more memory.Consumes less memory.
StorageString 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.

StringAs 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:

StringBuffer

StringBuffer 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 Differences Between String and StringBuffer

  1. The length of String object is fixed but the length of an object of StringBuffer can be increased when required.
  2. String object is immutable i.e. it’s object can’t be reassigned again whereas, the object of StringBuffer is mutable.
  3. String object is slower in performance whereas, the StringBuffer object is faster.
  4. String object consumes more memory whereas, StringBuffer objects consumes less memory.
  5. String objects are stored in a constant pool whereas, StringBuffer objects are stored on heap memory.

Conclusion

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.

Related Differences:

  1. Difference Between List and Tuple in Python
  2. Difference Between Structure and Union
  3. Difference Between extends and implements keywords in Java
  4. Difference Between Character Array and String
  5. Difference Between Interface and Abstract Class in Java & C#

Comments

  1. Joe says

    March 6, 2019 at 6:52 am

    In strings what happens when str+=”solution”; is given

    Reply
  2. Kundan kumar says

    June 25, 2019 at 5:20 am

    Nice article

    Reply
  3. Desmond Huang says

    July 16, 2020 at 6:47 am

    Very good explanations. Thanks a lot.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Top 10 Differences

  • Difference Between OLTP and OLAP
  • Difference Between while and do-while Loop
  • Difference Between Guided and Unguided Media
  • Difference Between Preemptive and Non-Preemptive Scheduling in OS
  • Difference Between LAN, MAN and WAN
  • Difference Between if-else and switch
  • Difference Between dispose() and finalize() in C#
  • Difference Between for and while loop
  • Difference Between View and Materialized View
  • Difference Between Server-side Scripting and Client-side Scripting

Recent Addition

  • Difference Between Java and Python
  • Difference Between PHP and HTML
  • Difference Between GPS and GNSS 
  • Difference Between Virtualization and Containerization
  • Difference Between Storage and Memory

Categories

  • Artificial Intelligence
  • DBMS
  • Hardware
  • Internet
  • Networking
  • Operating System
  • Programming
  • Software

Copyright © 2025 · Tech Differences · Contact Us · About Us · Privacy