Do you think IIT Guwahati certified course can help you in your career?
No
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.
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);
}
}
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);
}
}
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.
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.