Difference between StingBuilder and StringBuffer in java

Samad
3 min readDec 22, 2022

--

StringBuffer and StringBuilder are both classes in the Java programming language that allow you to create and manipulate strings. While both classes provide similar functionality, there are some key differences between them that you should be aware of. In this article, we’ll take a closer look at these differences and explore some examples of how to use StringBuffer and StringBuilder in your Java code.

One of the main differences between StringBuffer and StringBuilder is performance. StringBuilder is generally faster than StringBuffer because it is not thread-safe. StringBuffer is synchronized, which means that it can be safely accessed by multiple threads concurrently. This makes it slower than StringBuilder, but it also makes it more suitable for use in multi-threaded environments.

Here’s an example of how you might use StringBuilder to create and modify a string in your Java code:

StringBuilder sb = new StringBuilder();
sb.append("Hello, ");
sb.append("world!");
String greeting = sb.toString();

In this example, we create a new StringBuilder object and use the append method to add two strings together. The resulting string is then converted to a regular string using the toString method.

On the other hand, if you need to use a string in a multi-threaded environment, you may need to use StringBuffer to ensure thread safety. Here’s an example of how you might use StringBuffer to create and modify a string:

StringBuffer sb = new StringBuffer();

// Append a string to the StringBuffer
sb.append("Hello, ");

// Insert a string at a specific position
sb.insert(6, "world");

// Replace a section of the string with a new string
sb.replace(6, 11, "there");

// Delete a section of the string
sb.delete(6, 9);

// Reverse the string
sb.reverse();

// Convert the StringBuffer to a regular string
String greeting = sb.toString();

System.out.println(greeting); // Outputs "!dlrow ,olleH"

In this example, we create a new StringBuffer object and use a variety of methods to modify the string. We use the append method to add a string to the end of the buffer, the insert method to insert a string at a specific position, the replace method to replace a section of the string with a new string, the delete method to delete a section of the string, and the reverse method to reverse the order of the characters in the string. Finally, we use the toString method to convert the StringBuffer to a regular string

Here is an example of how you might use StringBuffer in a multi-threaded environment in Java:

class StringBufferExample {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();

// Create a thread that appends a string to the StringBuffer
Thread t1 = new Thread(new Runnable() {
public void run() {
sb.append("Hello, ");
}
});

// Create a thread that appends a string to the StringBuffer
Thread t2 = new Thread(new Runnable() {
public void run() {
sb.append("world!");
}
});

// Start the threads
t1.start();
t2.start();

// Wait for the threads to finish
try {
t1.join();
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}

// Convert the StringBuffer to a regular string
String greeting = sb.toString();

System.out.println(greeting); // Outputs "Hello, world!"
}
}

we create two threads that each append a string to the same StringBuffer object. Because StringBuffer is thread-safe, we can safely access the same object from multiple threads concurrently without worrying about thread interference or memory consistency errors. When the threads are finished, we use the toString method to convert the StringBuffer to a regular string and print it to the console.

--

--