Where to use StringBuffer/StringBuilder than String

18,220

Solution 1

Below is the main difference between these three most commonly used classes.

  • String class objects are immutable whereas StringBuffer and StringBuilder objects are mutable.
  • StringBuffer is synchronized while StringBuilder is not synchronized.
  • Concatenation operator "+" is internal implemented using either StringBuffer or StringBuilder.

Criteria to choose among String, StringBuffer and StringBuilder

  • If the Object value is not going to change use String Class because a String object is immutable.
  • If the Object value can change and will only be accessed from a single thread, use a StringBuilder because StringBuilder is unsynchronized.
  • In case the Object value can change, and will be modified by multiple threads, use a StringBuffer because StringBuffer is synchronized.

Solution 2

The main idea is that String is immutable. So when you are trying to modify it, new object created. StringBuffer and StringBuilder are mutable. And the difference is the first one is thread-safe.

The common approach to using StringBuilder is to parse something when you iteratively create a String object in a NON thread safe environment.

For example, you have a numbers array [1, 2, 3]. To create object "String1String2String3" you can use a StringBuilder

StringBuilder builder = new StringBuilder();
foreach(Integer num : array) {
    builder.append("String").append(num);
}
builder.toString();

It's better than just using String string = string + ("String" + num);. AFAIK, compiler will optimize using string concatenation in the loop to StringBuilder, but better to use it manually.

StringBuffer is used when you have shared states, that are modified by concurrent threads.

Solution 3

StringBuffers are thread-safe, meaning that they have synchronized methods to control access so that only one thread can access a StringBuffer object's synchronized code at a time. Thus, StringBuffer objects are generally safe to use in a multi-threaded environment.

StringBuilder's access is not synchronized so that it is not thread-safe. By not being synchronized, the performance of StringBuilder can be better than StringBuffer. Thus, if you are working in a single-threaded environment, using StringBuilder instead of StringBuffer may result in increased performance.

Share:
18,220
Soumyadip Das
Author by

Soumyadip Das

Java/Spring application Developer from Kolkata, India. Languages/Technology/Frameworks: Java/J2EE Servlet Spring Struts Web Tools: HTML JSP Flex XML json App server and DB: Tomcat MySQL Oracle Others: REST API Android Amazon AWS

Updated on July 03, 2022

Comments

  • Soumyadip Das
    Soumyadip Das almost 2 years

    Possible Duplicate:
    String, StringBuffer, and StringBuilder

    We know that String are immutable where StringBuffer/StringBuilder are mutable. But sometimes we get confused what to use in our code.. the String or StringBuffer/StringBuilder ?? Practically in our maximum code/quick code we use to prefer String than StringBuffer/StringBuilder.

    This question is to solve the confusion, if you have any idea & proper reason for that, then please give a reply.