Pages Navigation Menu

Coding is much easier than you think

Difference between String, StringBuffer and StringBuilder? When to use them?

Difference between String, StringBuffer and StringBuilder? When to use them?

 

The main difference between the three most commonly used String classes as follows.

 

  • StringBuffer and StringBuilder objects are mutable whereas String class objects are immutable.
  • StringBuffer class implementation is synchronized while StringBuilder class is not synchronized.
  • Concatenation operator “+” is internally implemented by Java using either StringBuffer or StringBuilder.

 
Immutable, means the value stored in the String object cannot be changed. So if String is immutable then how am I able to change the contents of the object?. Well, to be precise it’s not the same String object that reflects the changes you do. Internally a new String object is created to do the changes.

So suppose you declare a String object:

String myString = "Hello";

Next, you want to append “World” to the same String. What do you do?

myString = myString + " World";

When you print the contents of myString the output will be “Hello World”. Although we made use of the same object(myString), internally a new object was created in the process. So, if you were to do some string operation involving an append or trim or some other method call to modify your string object, you would really be creating those many new objects of class String.
 

Simple example to demonstrate that String object is immutable

 
Below is a simple example that will make you believe that what I said about String object is indeed true!

String myString = "Let's test";
myString.concat(" if the String object is MUTABLE");

System.out.println(myString);

myString = myString.concat("if the String object is IMMUTABLE");
System.out.println(myString);

 

Output :

 

Let's test

Let's test if the String object is IMMUTABLE

 
That’s all people! The above piece of code proves that String is immutable and hence the results of operations like concat etc. should be stored into a new object.
 
Now isn’t that a performance issue?

Yes, it definitely is.

Then how do you make your string operations efficient?

By using StringBuffer or StringBuilder.

How would that help?

Well, since StringBuffer/StringBuilder objects are mutable, we can make changes to the value stored in the object. What this effectively means is that string operations such as append would be more efficient if performed using StringBuffer/StringBuilder objects than String objects.
 

Finally, whats the difference between StringBuffer and StringBuilder?

StringBuffer and StringBuilder have the same methods with one difference and that’s of synchronization. StringBuffer is synchronized( which means it is thread safe and hence you can use it when you implement threads for your methods) whereas StringBuilder is not synchronized( which implies it isn’t thread safe).

So, if you aren’t going to use threading then use the StringBuilder class as it’ll be more efficient than StringBuffer due to the absence ofsynchronization.
 

Criteria to choose among String, StringBuffer and StringBuilder

 

  • If the Object value will not change in a scenario use String Class because a String object is immutable.
  • If the Object value can change and will only be modified from a single thread, use a StringBuilder because StringBuilder is unsynchronized(means faster).
  • If the Object value may change, and can be modified by multiple threads, use a StringBuffer because StringBuffer is thread safe(synchronized).

About Mohaideen Jamil


Am currently working as a Struts 2 Developer in a reputed IT Organisations. I can help you with teaching Core java and Struts 2. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

  • Aswathi

    thankyou very much.my all doubts are cleared now.very well explained in simple language.

  • Rahamat

    ultimate explaination.. thanks