×

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.

StringBufferStringBuilder
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.


Related Topics

What is new in Java 17?

Java 17 LTS is the most recent long-term support release for the Java SE platform. Under the Oracle No-Fee Terms and Conditions License, which stands for long-term support, JDK 17...

6 minutes read.

Java Comparator Interface

Java comparator interface is used in a situation when we have to sort an object which does not implement Comparable or do sorting in a different way than the Comparable....

1 minute read.

String Palindrome Program in Java

String Palindrome Program in Java The palindrome is a string, phrase, word, number, or other sequences of characters that can be read in both directions i.e. forward (left to right) and...

11 minutes read.

Loose Coupling in Java

Loosely coupling mechanism in java means one reference of a variable capable of holding multiple implementation class memory is called loosely coupling. Or in other words, one interface reference variable...

3 minutes read.

Bin Packing Problem Java

Assigning some items with known weights to bins with consistent capacities is necessary for the bin packing problem. The goal is to use the smallest possible boxes while ensuring everything is put...

6 minutes read.

String vs StringBuilder

String vs StringBuilder In this section, we will discuss the comparison between Java String and StringBuilder class. String In Java, a string is treated as an object that represents a sequence of characters....

4 minutes read.

How to write basic Java Programs

Java Basic Programs In this section, we will learn how to write basic Java programs. But first we need to take care of the following requirement list. To execute a Java program,...

4 minutes read.

Java Full Stack

A person who can create both the front end and back end of an application is a full-stack developer. In essence, the term "Java full-stack" refers to a web developer...

9 minutes read.

List of Constants in Java

In this tutorial, we are going to deal with constants available in Java.Every programming language has its constants. Similarly, Javaalso has got constants of its own. In this tutorial, we will...

6 minutes read.

How to Convert Octal to Decimal in Java

How to Convert Octal to Decimal in Java There are two methods to convert Octal to Decimal: Using parseInt() method Using user-defined logic Using Integer.parseInt() method The Integer.parseInt() method is a static method...

2 minutes read.

Package Program in Java

Package Program in Java The package program in Java helps us to understand the significance of packages in Java. Packages are mainly used to group together similar classes, interfaces and sub-packages....

6 minutes read.

Static class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM is mostly...

7 minutes read.

How to Split the String in Java with Delimiter

In Java, splitting strings is a significant and typically used activity while coding. Java gives different ways of dividing the String. The most widely recognized way is to use the...

3 minutes read.

Deadlock in Java

Deadlock is when two or more processes wait for the state to do their tasks, but none of them can do so. It is a very common problem that one...

6 minutes read.

Isomorphic String in Java

In this tutorial, we will understand what is meant by isomorphic String in java. We will also see a Java program to find out if the string is isomorphic or...

4 minutes read.

XNOR operator in Java

The opposite of the binary equivalent of XOR is given by XNOR. Truth table: XYXNOR001010100111 If the bits are the same, it returns 1, else it returns 0. Examples:  Input: 10 20 Output : 1 A Binary...

3 minutes read.

Java Command Line Argument

Command-line arguments are passed to the main() method when we want to pass information into a program during runtime. It is the information that directly follows the program’s name on the...

1 minute read.

How to Read CSV Files in Java?

Comma-Separated Values is the acronym for this format. It is a straightforward file format storing textual tabular data in a database or spreadsheet. The CSV files can be imported into...

3 minutes read.

Java Math nextAfter() Method

The nextAfter() method of Math class returns the floating-point value adjacent to the first argument in direction of the second argument. Syntax: public static double nextAfter (double start, double direction)public static float...

2 minutes read.

How to Split String by Comma in Java

strsplit() technique permits you to break a string given the explicit Java string delimiter. The Java string split property is frequently a space or a comma(,) that you want to...

7 minutes read.