×

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

Replace character in string Java

Characters in Java In the package of Java language, there is a container class called Character. A single field of type char is contained in a Character object. For manipulating characters,...

4 minutes read.

Dangling Else problem in Java

A language interpretation uncertainty is the hanging other issue. The following two types of condition executed code are both possible in programming: 1. if-then-else form 2. if-then form When dealing with the nested...

3 minutes read.

Best Java IDE

Applications for desktop, workplace, smartphone, and the internet can be created using Java, one of the most popular programming languages. Java will undoubtedly be a popular programming language for so...

5 minutes read.

Shopping Bill in Java

Java Shopping Bill Here in this program, we are about to create a JAVA class called Products which will have some properties or attributes like prod_name (Product Name ), qty (quantity),...

4 minutes read.

Java SE vs EE

Java : Java is an independent platform. It works on any kind of operating system. We use java to develop and to focus on large or major projects. The goal of...

3 minutes read.

Object Definition in Java

In this article, you will be acknowledged about object in Java, its definition and how is this useful in writing the programs in Java. The comprehension of object-oriented technology depends on...

4 minutes read.

Morris Traversal for Preorder in Java

Without the use of recursion or stacks, we traverse a tree using the Morris algorithm. The linked binary tree is the foundation of the Morris traversal. Preorder Morris Traversal Algorithm The preorder...

3 minutes read.

Factorial Program in Java using Recursion

Factorial Program in Java Factorials are used in mathematics to calculate permutations and combinations. It is denoted by the exclamatory symbol (!). Suppose, p is a number whose factorial is to...

3 minutes read.

Java Package Keyword

A collection of classes, interfaces, and subpackages in a Java program are referred to as a package. Here, we'll learn in-depth how to make and use user-defined packages. package is a...

6 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.

Java Long Keyword

Long is a primitive data type in Java. To initilize variables, we implement the long keyword. It can also be applied to techniques. A 64-bit two's complement integer can put...

3 minutes read.

Array and String based questions in Java

1. What is an Array in Java? A collection of identical data types is referred to as an array. There can be no separate data kinds. It supports the storage of...

4 minutes read.

Java String Inbuilt functions

There are different types of inbuilt functions available in the Java String class, all of them are listed below. Char chat At (int index)It outputs the character indicated by the index....

5 minutes read.

Java Coding Software

Desktop and web apps are created using Java, an object-oriented programming language. Java code may be executed on any platform, making it platform-independent. A text editor, tool, or piece of...

9 minutes read.

Java Try Keyword

The try block in Java is used to run essential code, such as connection closure, among other things. Whether an exception is resolved or not, the Java try block has...

3 minutes read.

Producer consumer problem in Java using Synchronised block

The producer-consumer problem in Java, commonly known as the bounded-buffer problem, is a well-known multi-process synchronization challenge where we attempt to synchronize many processes. Two processes are involved in the producer-consumer problem:...

3 minutes read.

Difference Between Access Specifiers and Modifiers in Java

Java employs access modifiers to restrict a class's data members, member functions, and constructor. Access modifiers are essential when creating Java program and applications. Access modifiers in Java include: defaultpublicprotectedprivate Default Access Modifiers Without...

4 minutes read.

How to add 4 Years to the Current Date in Java?

In this tutorial, we will learn how to add 4 years to the local or current date in Java language. We will begin our topic with basic concepts and would...

2 minutes read.

Structure of Java Program

Java is well-known as an object-oriented, secure, and platform-independent programming language. The Java programming language allows us to construct a wide variety of programs. Therefore, it is essential to fully...

6 minutes read.

Int vs Integer in Java

In Java, numerical data can be stored using int or Integer. int and Integer both have different definitions but similar usage in Java. What is int in Java? int is a primitive...

4 minutes read.