String vs StringBuffer vs StringBuilder. Example function for finding which one is better.

String vs StringBuffer vs StringBuilder. Example function for finding which one is better.

In this article, we will be going to explore when to use String, StringBuffer, and StringBuilder.

String
Everybody knows that String is an immutable class. if someone may not know about immutable. For those folks, I am explaining here about immutable. The general meaning of immutable is unchangeable. You can’t change that. So, the String object is unchangeable. For Example.
String s1 = new String(“Suresh “); // here we are creating new String instance.

After creating a new instance of String Object, I am trying to modify the same string object. Yes, it is working perfectly and I am getting the expected output
But, the problem here is how appending is happening. When using the + operator each time, it is creating a new String object. In real-time we may get a scenario to append 1000 of String object or lacs of String object to append. This creates huge performance issues so that, all Java experts saying that we should not use String objects while appending data.

Java provides two utility classes to handle the String data. One is StringBuffer and another one is StringBuilder.
StringBuffer
StringBuffer is more efficient than String class because it is a mutable object. When we perform any string manipulation better we may choose StringBuffer. But, StringBuffer is a thread-safe meaning i.e, the append method from StringBuffer is a synchronized method. The following is the append() method from StringBuffer. Note that, it is a Synchronized method.

At a time one thread can access StringBuffer Object so it is not much faster when compared to StringBuilder.
StringBuilder
StringBuilder is more efficient than StringBuffer because StringBuilder is not a thread-safe meaning, it is not a synchronized Method. So, Multiple threads can access StringBuilder. StringBuilder is really faster than StringBuffer. One thing you need to consider. if you are Program is dealing with Thread, you should not use StringBuilder. it may corrupt the data that you handling in the program so, in such a situation you need to choose StringBuffer. The following code snippet showing append() method implementation from StringBuilder. Note that it is not a Synchronized method

I have written a small program to find out the performance of String, StringBuffer, and StringBuilder

Appending String using + operator

The plus operator is taking 29 seconds to perform the following operation

Appending String using the append() Method using StringBuffer

StringBuffer is taking less than a second. it is taking 12000000 Nano Seconds

Appending String using the append() method using StringBuilder

StringBuilder is taking less than a second. it is taking 10000000 Nano Seconds

Based on the above program output, we can decide that StringBuilder gives better performance however based on your program requirement, you need to choose either StringBuffer or StringBuilder but you should not go with String for appending operation.

Close Menu
×
×

Cart