Java StringBuffer vs StringBuilder
Java StringBuffer vs StringBuilder
StringBuffer
The StringBuffer class is used to create mutable string. StringBuffer shows writable and growable character sequences.
Java StringBuffer program
FileName: StringBufferExample.java
// A basic program that demonstrates the working of StringBuffer public class StringBufferExample { // main method public static void main(String[] argvs) { // creating an object of the class StringBuffer StringBuffer sb = new StringBuffer("Hi "); // appending the string Tutorial & Example sb.append("Tutorial & Example "); // displaying the output System.out.println(sb); } }
Output:
Hi Tutorial & Example
StringBuilder
The StringBuilder class is also used to create mutable string. The StringBuilder is same as the StringBuffer class. The difference is StringBuilder class is not synchronized whereas the StringBuffer class is synchronized
Java StringBuilder Program
FileName: StringBuilderExample.java
// A basic program that demonstrates the working of StringBuilder public class StringBuilderExample { // main method public static void main(String[] argvs) { // creating an object of the class StringBuffer StringBuilder sb = new StringBuilder("Hi "); // appending the string Tutorial & Example sb.append("Tutorial & Example "); // displaying the output System.out.println(sb); } }
Output:
Hi Tutorial & Example
Difference Between StringBuffer and StringBuilder
The following table describes the key differences between StringBuffer and StringBuilder in Java.
StringBuffer | StringBuilder |
StringBuffer is a mutable, thread-safe, and synchronized. Thread-safe means two or more than two threads are not allowed to access the same method of the StringBuffer class simultaneously. | StringBuilder class is also a mutable but it is not thread-safe and not synchronized. Thread-safe means two or more than two threads are allowed to access the same method of the StringBuilder class simultaneously. |
StringBuffer is not that much efficient as compared to StringBuilder. | StringBuilder performs better, i.e., more efficient as compared to StringBuffer. |
Performance Comparison Between StringBuffer and StringBuilder class
We know that StringBuilder is quick as compared to StringBuffer. Let’s check it with the help of a Java program.
FileName: PerformanceTestExample.java
// A basic Java program that compares the performance of StringBuffer and // StringBuilder public class PerformanceTestExample { // main method public static void main(String argvs[]) { long sTime = System.currentTimeMillis(); // creating an object of StringBuffer StringBuffer strBuffer = new StringBuffer("Hi "); for (int j = 0; j < 10000; j++) { // doing the concatenation work strBuffer.append("Tutorial & Example"); } // displaying the time taken by StringBuffer System.out.println("Total Time consumed by the StringBuffer is: " + (System.currentTimeMillis() - sTime) + "ms"); // resetting the start time sTime = System.currentTimeMillis(); // creating an object of StringBuilder StringBuilder strBuilder = new StringBuilder("Hi "); for (int j = 0; j < 10000; j++) { // doing the concatenation work strBuilder.append("Tutorial & Example"); } // displaying the time taken by StringBuilder System.out.println("Total Time consumed by the StringBuilder is: " + (System.currentTimeMillis() - sTime) + "ms"); } }
Output:
Total Time consumed by the StringBuffer is: 4ms Total Time consumed by the StringBuilder is: 3ms
Explanation: Observe the output, we see that StringBuilder is quicker as compared to StringBuffer.