Open In App

Difference Between StringBuffer and StringBuilder in Java

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Difference-between-StringBuffer-and-StringBuilder-in-Java

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);
    }
}


Output:

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);
    }
}


Output:

Hello World!

Conversion from StringBuffer to StringBuilder

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);
    }
}


Output:

Geeks

Conversion from StringBuilder to StringBuffer

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);
    }
}


Output:

Geeks

StringBuilder vs StringBuffer in Java

StringBuffer Class StringBuilder Class
StringBuffer is present in Java. StringBuilder was introduced in Java 5.
StringBuffer is synchronized. This means that multiple threads cannot call the methods of StringBuffer simultaneously. StringBuilder is asynchronized. This means that multiple threads can call the methods of StringBuilder simultaneously.
Due to synchronization, StringBuffer is called a thread safe class. Due to its asynchronous nature, StringBuilder is not a thread safe class.
Due to synchronization, StringBuffer is lot slower than StringBuilder. Since there is no preliminary check for multiple threads, StringBuilder is a lot faster than StringBuffer.


Last Updated : 02 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads