Difference Between StringBuffer and StringBuilder in Java - GeeksforGeeks

02 Jul, 2020
Strings in Java are the objects that are backed internally by a char array. Since arrays are immutable(cannot grow), Strings are immutable as well. Whenever a change to a String is made, an entirely new String is created. However, java provides multiple classes through which strings can be used. Two such classes are StringBuffer and StringBuilder. In this article, we will see the difference between both the classes.
StringBuffer Class: StringBuffer is a peer class of String that provides much of the functionality of strings. The string represents fixed-length, immutable character sequences while StringBuffer represents growable and writable character sequences. StringBuffer may have characters and substrings inserted in the middle or appended to the end. It will automatically grow to make room for such additions and often has more characters preallocated than are actually needed, to allow room for growth. In order to create a string buffer, an object needs to be created, (i.e.), if we wish to create a new string buffer with name str, then:
StringBuffer str = new StringBuffer();

Example: The following is an example which implements the StringBuffer class.
// Java program to demonstrate// the StringBuffer class  public class GFG {      // Driver code    public static void main(String args[])    {          // Creating a new StringBuffer        StringBuffer str            = new StringBuffer("Hello");          str.append(" World!");          System.out.println(str);    }}
Hello World!

StringBuilder Class: Similar to StringBuffer, the StringBuilder in Java represents a mutable sequence of characters. Since the String Class in Java creates an immutable sequence of characters, the StringBuilder class provides an alternative to String Class, as it creates a mutable sequence of characters. The function of StringBuilder is very much similar to the StringBuffer class, as both of them provide an alternative to String Class by making a mutable sequence of characters. Similar to StringBuffer, in order to create a new string with the name str, we need to create an object of StringBuilder, (i.e.):
StringBuilder str = new StringBuilder(); 

Example: The following is an example which implements the StringBuilder class.
// Java program to demonstrate// the StringBuilder class  public class GFG {      // Driver code    public static void main(String args[])    {          // Creating a new StringBuilder        StringBuilder str            = new StringBuilder("Hello");          str.append(" World!");          System.out.println(str);    }}
Hello World!

The StringBuffer cannot be directly converted to StringBuilder. We first need to convert the StringBuffer to a String object by using the inbuilt method toString(). After converting it to a string object, we can simply create a StringBuilder using the constructor of the class. For example:
// Java program to demonstrate// the conversion between the// StringBuffer and StringBuilder// class  public class GFG {      // Driver code    public static void main(String args[])    {        StringBuffer sbr            = new StringBuffer("Geeks");          // Conversion from StringBuffer        // object to the String object        String str = sbr.toString();          // Creating a new StringBuilder        // using the constructor        StringBuilder sbl            = new StringBuilder(str);          System.out.println(sbl);    }}
Geeks

Similar to the above conversion, the StringBuilder cannot be converted to the StringBuffer directly. We first need to convert the StringBuilder to the String object by using the inbuilt method toString(). Now, we can create a StringBuilder using the constructor. For example:
// Java program to demonstrate// the conversion between the// StringBuilder and StringBuffer// class  public class GFG {      // Driver code    public static void main(String args[])    {        StringBuilder sbr            = new StringBuilder("Geeks");          // Conversion from StringBuilder        // object to the String object        String str = sbr.toString();          // Creating a new StringBuffer        // using the constructor        StringBuffer sbl            = new StringBuffer(str);          System.out.println(sbl);    }}
Geeks

java-StringBuffer
Java-StringBuilder
Difference Between
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Difference between var, let and const keywords in JavaScript
Difference Between Method Overloading and Method Overriding in Java
Difference between Internal and External fragmentation
Difference between Prim's and Kruskal's algorithm for MST
Difference between HashMap and HashSet
Arrays in Java
Split() String method in Java with examples
For-each loop in Java
Arrays.sort() in Java with examples
Reverse a string in Java