Java – StringBuffer v/s StringBuilder

In this article, we will discuss difference between StringBuffer and StringBuilder in detail with example

Both are mutable sequence of characters to replace String’s immutability to make memory efficient when dealing with string handling but there are differences between them

Let us move forward and discuss difference between them;

1. StringBuffer v/s StringBuilder:

StringBuffer

StringBuilder

StringBuffer is mutable and synchronizedStringBuilder is also mutable but it is non-synchronized
All methods of StringBuffer class are synchronizedMethods of StringBuilder class are non-synchronized
That’s 2 or more threads aren’t allowed to operate on same StringBuffer object simultaneouslyWhereas StringBuilder allows multiple threads to be operated on the same StringBuilder object
In simple word, it is thread-safe while working in a multi-threaded environmentIt is not thread-safe while working in a multi-threaded environment
Due to synchronization, performance gets a big hit;

 

because every thread has to acquire and release locks before any operations on StringBuffer object

Due to non-synchronization, performance is relatively faster than StringBuffer;

 

As no thread has to wait to acquire and release locks which is overhead in case of StringBuffer

Q) When to use StringBuffer and StringBuilder:

StringBuffer:

  • In a multi-threaded application, StringBuffer need to be preferred as it ensure thread-safety
  • Though it will be slower when compared with StringBuilder but ensures data-consistency by not allowing multiple threads to operate at the same time concurrently
  • Because every thread has to acquire lock before any operation on StringBuffer object and only after releasing the lock, 2nd thread can take charge

StringBuilder:

  • In a single threaded application, StringBuilder is a very apt choice as it is doesn’t require thread-safety
  • And also it will get rid of acquiring and releasing locks and hence performance will be improved effectively when compared with StringBuffer

2. Example on performance of StringBuilder v/s StringBuffer:

StringBufferVsStringBuilderPerformanceTest.java

package in.bench.resources.performance.test;

public class StringBufferVsStringBuilderPerformanceTest {

	public static void main(String[] args) {

		// counter
		int iCount = 0;
		long startTime = 0;
		long endTime = 0;

		// sample StringBuffer
		StringBuffer buffer = new StringBuffer();

		// START time for StringBuffer append() operation
		startTime = System.currentTimeMillis();

		// Test 1: for StringBuffer append
		while(iCount < 50000) {

			buffer.append("BenchResources.Net");

			// increment counter by 1
			iCount++;
		}

		// END time for StringBuffer append() operation
		endTime = System.currentTimeMillis();

		System.out.println("The time taken for "
				+ "StringBuffer append() is : "
				+ (endTime - startTime) + "ms");

		// sample StringBuilder
		StringBuilder builder = new StringBuilder();

		// START time for String concatenation
		startTime = System.currentTimeMillis();

		// assign ZERO to counter
		iCount = 0;

		// Test 2: for StringBuilder append() operation
		while(iCount < 50000) {

			builder.append("BenchResources.Net");

			// increment counter by 1
			iCount++;
		}

		// END time for String concatenation
		endTime = System.currentTimeMillis();

		System.out.println("The time taken for "
				+ "StringBuilder append() is : "
				+ (endTime - startTime) + "ms");
	}
}

Output:

The time taken for StringBuffer append() is : 11ms
The time taken for StringBuilder append() is : 5ms

Note:

  • There will be slight difference in the result on executing at various times
  • StringBuilder more faster than StringBuffer, as it is non-synchronized

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - String v/s StringBuffer v/s StringBuilder
Java - String v/s StringBuffer