Java provides different classes that you can leverage when working with Strings. Some of these classes include StringBuffer
and StringBuilder
. Some of the common features you can use with these classes are to append or insert a String to another String. Nonetheless, how do you know the difference between these classes and how to apply them? In this tutorial, you will learn the difference between StringBuffer
and StringBuilder
.
StringBuffer
One of the attributes of StringBuffer
is that it is thread-safe. Thread safety is a result of using synchronization when processing a String. Synchronization ensures threads access a String in an orderly manner. As a result, problems associated with inconsistent data do not occur.
On the negative side, processing the String will be slow since threads have to wait for their turn.
StringBuilder
One of the main attributes of StringBuilder
is that it does not use synchronization. This makes it a fast option where thread safety is not a concern.
On the negative side, avoid using this class when thread safety is a concern. This will result in inconsistent data in your application.
StringBuffer vs StringBuilder
As of now, the main difference between the two classes is quite obvious. You can see that StringBuffer
uses synchronization while StringBuilder
does not. This is the main factor to consider when deciding which class to use. Additionally, there are other minor differences.
The example below shows how the two classes differ with regard to synchronization.
package com.javawhizz; public class Main { public static void stringBuilder(int count){ StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < count; i++) { stringBuilder.append("javawhizz"); } String javawhizz = stringBuilder.toString(); } public static void stringBuffer(int count){ StringBuffer stringBuffer = new StringBuffer(); for (int i = 0; i < count; i++) { stringBuffer.append("javawhizz"); } String javawhizz = stringBuffer.toString(); } public static void main(String[] args) { int count = 1000000; long start = System.nanoTime(); stringBuilder(count); long stop = System.nanoTime(); long stringBuilderResult = stop - start; start = System.nanoTime(); stringBuffer(count); stop = System.nanoTime(); long stringBufferResult = stop - start; System.out.println(("Time taken using StringBuilder: " + stringBuilderResult)+" nanoseconds"); System.out.println(("Time taken using StringBuffer: " + stringBufferResult + " nanoseconds")); } }
Output
Time taken using StringBuilder: 77224800 nanoseconds Time taken using StringBuffer: 97370000 nanoseconds
This code uses the append()
method of the classes to append a String using finite iterations. The System.nanoTime()
method helps to calculate the time it takes to append the String. To realize this, call the method before and after the method to perform append. Finally, calculate the time difference and log it to the console.
From this code, you can see that StringBuilder
is faster than StringBuffer
. This is because StringBuilder
does not use synchronization.
Conclusion
In this tutorial, you have learned the difference between StringBuffer
and StringBuilder
. In general, Use StringBuilder
if thread safety is not a concern but performance is. Otherwise, use StringBuffer
if performance is not a concern but thread safety is.
Happy Hacking!
0 Comments