group-icon
Java Developers
Group for Java developers and programmers. Ask questions and get answers related to Java programming
254 Members
Join this group to post and comment.
DHIRAJ LOTLIKAR
DHIRAJ LOTLIKAR
Information Technology
12 Feb 2019

How StringBuffer and StringBuilder differ from String Class?

String objects created using java.lang.String class are immutable. Once they are created, they can not be changed. If you try to change or modify them, a new string object will be created with modified content. This property of String class may affect some memory for applications which need frequent modification of string objects. To overcome this two more classes are introduced in Java to represent the strings. They are StringBuffer and StringBuilder. Both these classes are also members of  java.lang package same as String class.

1) Immutability :
As objects of String class are immutable, objects of StringBuffer and StringBuilder class are mutable. You can change the contents of StringBuffer and StringBuider objects at any time of execution.

2) Object Creation :
You have to use ‘new‘ operator to create objects to StringBuffer and StringBuilder classes.

3) Storage Area :
Objects are stored in heap memory. Where as objects of String class are created using both string literals and new operator, they are stored in string constant pool as well as heap memory.


4) Performance :

Because of thread safety property of String and StringBuffer classes, they reduces the performance of multithreaded applications.

5) String Concatenation :

There will be serious performance issues when you are performing lots of string concatenation using String class. But, if you use either StringBuffer or StringBuilder instead of String class, your application will perform better.

Archana Priyadarshini
Archana Priyadarshini
Computer Science
1mo ago

String

String is immutable ( once created can not be changed )object . The object created as a String is stored in the Constant String Pool.

Every immutable object in Java is thread safe ,that implies String is also thread safe . String can not be used by two threads simultaneously.String once assigned can not be changed.

StringBuffer

StringBuffer is mutable means one can change the value of the object . The object created through StringBuffer is stored in the heap. StringBuffer has the same methods as the StringBuilder , but each method in StringBuffer is synchronized that is StringBuffer is thread safe .Due to this it does not allow two threads to simultaneously access the same method . Each method can be accessed by one thread at a time .

But being thread safe has disadvantages too as the performance of the StringBuffer hits due to thread safe property . Thus StringBuilder is faster than the StringBuffer when calling the same methods of each class.

String Buffer can be converted to the string by using toString() method.

StringBuffer demo1 = new StringBuffer("Hello") ;

The above object stored in heap and its value can be changed .

demo1=new StringBuffer("Bye");

 Above statement is right as it modifies the value which is allowed in the StringBuffer

StringBuilder

StringBuilder is same as the StringBuffer , that is it stores the object in heap and it can also be modified . The main difference between the StringBuffer and StringBuilder is that StringBuilder is also not thread safe.StringBuilder is fast as it is not thread safe .

StringBuilder demo2= new StringBuilder("Hello");

The above object too is stored in the heap and its value can be modified

demo2=new StringBuilder("Bye");

Above statement is right as it modifies the value which is allowed in the StringBuilder