Table of contents
1.
Introduction
2.
What is StringBuffer class?
2.1.
Implementation in a code
3.
What is Stringbuilder Class?
3.1.
Implementation in a code
4.
Conversion from StringBuffer to StringBuilder
5.
Conversion from StringBuilder to StringBuffer
6.
StringBuffer vs StringBuilder in Java
7.
Frequently Asked Questions
7.1.
Which is faster StringBuilder or buffer?
7.2.
Why StringBuffer is slower than StringBuilder?
7.3.
Why do we use buffer and builder classes in java?
7.4.
Why is String builder more efficient?
8.
Conclusion
Last Updated: Aug 13, 2024
Easy

Difference between StringBuffer and StringBuilder Class

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In Java, strings are objects representing a sequence of character values. An array of characters work the same as a string in Java. Since you cannot extend arrays, i.e., they are immutable, strings are immutable as well. We can use classes in Java like StringBuilder and StringBuffer to provide us with the functionality of immutable strings. StringBuilder does not have thread safety but has faster implementation. StringBuffer provides strings that are safe to use with multiple threads. This article will show the difference between the StringBuilder and StringBuffer classes.

Difference between StringBuffer and StringBuilder Class

Also see about  Iteration Statements in Java, and Duck Number in Java.

What is StringBuffer class?

StringBuffer is a string class that provides much of the functionality of mutable strings. StringBuffer makes strings growable and writable. StringBuffer can either have characters or substrings inserted in the middle or appended to the end. It grows to make room for additions and has more characters preallocated than are needed to allow room for growth. To create a string buffer, an object needs to be created.

StringBuffer str = new StringBuffer(); 

Implementation in a code

The following is an example which implements the StringBuffer class.

Code:

public class Ninja {
    public static void main(String args[])
    {
        StringBuffer str = new StringBuffer("Coding");
        str.append(" Ninjas!");
        System.out.println(str);
    }
}

 

Output:

Coding Ninjas!

Also see, Swap Function in Java

What is Stringbuilder Class?

This class is similar to StringBuffer in Java, representing a mutable sequence of characters. The StringBuilder class provides an alternative to String Class, i.e., creating a mutable sequence of characters. The function of StringBuilder is also much similar to the StringBuffer class. To create a new string with the name str, we need to create an object of StringBuilder.

StringBuilder str = new StringBuilder(); 

Implementation in a code

The following is an example which implements the StringBuilder class.

Code:

public class Ninja {
    public static void main(String args[])
    {
        StringBuilder str = new StringBuilder("Coding");
        str.append(" Ninjas!");
        System.out.println(str);
    }
}

 

Output:

Coding Ninjas!

 

You can also read about the topic of Java Destructor and Hashcode Method in Java.

Conversion from StringBuffer to StringBuilder

StringBuffer can be converted to StringBuilder function as shown in the following example:

Code:

public class Ninja {
    public static void main(String args[])
    {
        StringBuffer sbr = new StringBuffer("Ninjas");
        String str = sbr.toString();
        StringBuilder sbl = new StringBuilder(str);
        System.out.println(sbl);
    }
}

 

Output:

Ninjas

 

Explanation:

In the above code, we first converted the StringBuffer to a String object using the built-in method toString() because we cannot directly convert the StringBuffer to StringBuilder. After converting it to a string object, we created a StringBuilder using the class's constructor. 

Conversion from StringBuilder to StringBuffer

StringBuilder can be converted to StringBuffer function as shown in the following example:

Code:

public class Ninja {
    public static void main(String args[])
    {
        StringBuilder sbl = new StringBuilder("Ninjas");
        String str = sbl.toString();
        StringBuffer sbr = new StringBuffer(str);
        System.out.println(sbr);
    }
}

 

Output:

Ninjas

 

Explanation:

In the above code, the StringBuilder cannot be converted to the StringBuffer directly. So, we first converted the StringBuilder to the String object using the built-in method toString(). Then we created a StringBuffer using the constructor. Try this code by yourself on Online Java Compiler.

StringBuffer vs StringBuilder in Java

Let us look at the differences between StringBuffer and StringBuilder classes in Java.

StringBuffer Class  StringBuilder Class 
StringBuffer was introduced in Java 1.0 StringBuilder was introduced in Java 1.5
Multiple threads cannot call the methods of StringBuffer simultaneously, i.e.,it is synchronized. Multiple threads can call the methods of StringBuilder simultaneously, i.e., it is asynchronised.
StringBuffer is called a thread safe class.  StringBuilder is not a thread safe class.
StringBuffer is lot slower than StringBuilder, therefore, less efficient.  StringBuilder is a lot faster than StringBuffer, therefore, more efficient.

 

Learn more, Difference Between IOT and M2M

Frequently Asked Questions

Which is faster StringBuilder or buffer?

StringBuilder is generally faster than StringBuffer because it is not synchronized, making it more efficient in single-threaded environments where synchronization is unnecessary.

Why StringBuffer is slower than StringBuilder?

StringBuilder is faster than StringBuffer because it's not synchronized.

Why do we use buffer and builder classes in java?

String is immutable whereas StringBuffer and StringBuilder are mutable classes. So they are used to make strings mutable.

Why is String builder more efficient?

StringBuilder is a lot faster than StringBuffer, therefore, more efficient.

Conclusion

In this article, we have extensively discussed StringBuffer and StringBuilder classes and their differences with different code examples. Having gone through this article, I am sure you must be excited to read similar blogs. Coding Ninjas has got you covered. Here are some similar blogs to redirect: StringBuilder ClassStringBuffer ClassConverting Java strings to intUnderstanding strings in javaMethod to take input in javaint to string conversion in java We hope that this blog has helped you enhance your knowledge, and if you wish to learn more, check out our Coding Ninjas Blog site and visit our Library. Here are some courses provided by Coding Ninjas: Basics of C++ with DSACompetitive Programming and MERN Stack Web Development. Do upvote our blog to help other ninjas grow. Happy Learning!
 

Recommended problems -

Live masterclass