Last Updated: 19 August, 2023
In Java, StringBuffer and StringBuilder are java.lang package classes used to represent a sequence of characters. Both classes are similar in many ways, such as that both are mutable (changeable) strings and, by default, final classes, etc.
Apart from the similarities, there are many differences between the StringBuffer and StringBuilder in Java, which are given below.
StringBuffer is a mutable, final, and synchronized (thread-safe) Java class that was introduced in the JDK 1.0 version and is available in the java.lang package.
The StringBuffer class is used to create a mutable String object (the object can be changed once created). StringBuffer is similar to the String class except for one major difference: StringBuffer is mutable, whereas String is immutable (an object cannot be changed once it is created).
The StringBuffer class is thread-safe, which means that at any time, multiple threads cannot access it simultaneously. So it is safe and will result in order, but implementing the synchronization mechanism will take some time for the JVM, which is why StringBuffer is a little slower than StringBuilder.
Java StringBuffer Class Declaration
public final class StringBuffer
extends Object
implements Serializable, Comparable <StringBuffer>, CharSequence
StringBuilder is mutable, non-synchronized, and a final class in Java. It was introduced in the JDK 1.5 version and placed in the java.lang package.
StringBuilder is used to create a mutable string object, which means we can modify the string without creating a new object. StringBuilder provides no guarantee of thread safety as it is non-synchronized at a time when multiple threads can access the StringBuilder object.
StringBuilder class description
public final class StringBuilder extends Object
implements Serializable, CharSequence
StringBuffer | StringBuilder |
---|---|
The StringBuffer object is thread-safe and synchronized. Multiple threads cannot call the methods of StringBuffer simultaneously. | The StringBuilder object is not synchronized and is not thread-safe. Multiple threads can call the methods of StringBuilder simultaneously. |
StringBuffer is suitable for multi-threaded environments. | StringBuilder is suitable for single-threaded environments. |
StringBuffer performance is slower as compared to StringBuilder. | StringBuilder performance is faster as compared to StringBuffer. |
StringBuffer has been available since the JDK 1.0 version. | StringBuilder has been available since JDK Version 5. |
Output
Welcome in StringBuffer Class
Output
Welcome in StringBuilder Class
One of the major differences between StringBuffer and StringBuilder is the performance. StringBuffer is a little slower than StringBuilder because StringBuffer methods are synchronized, while StringBuilder methods are not.
Let's check the performance of the StringBuffer and StringBuilder classes with the help of the given example.
Output
Time taken by StringBuffer: 3ms
Time taken by StringBuilder: 2ms
To convert StringBuffer to StringBuiler, first we need to convert StringBuffer to a String object by using the toString() method, and then we will create StringBuilder and pass the String object into the constructor.
Let's see an example:
Output
StringBuilder value: JavaByTechie
Same as StringBuffer to StringBuiler conversion process, first we need to convert StringBuilder to a String object by using the toString() method, and then we will create StringBuffer and pass the String object into the constructor.
Let's see an example:
Output
StringBuffer value: JavaByTechie
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com