Skip to main content

See also:

When to use the Java String, StringBuffer and StringBuilder classes

Java provides three useful classes for string objects: String, StringBuilder and StringBuffer.
Java provides three useful classes for string objects: String, StringBuilder and StringBuffer.
freeimages.com/shho

Java provides three useful classes for string objects: String, StringBuilder and StringBuffer. All of these classes effectively represent string objects. Each class has a specific purpose. Depending on your requirements, one class may be a better choice than the others. The examples below describe when to use the Java String, StringBuilder and StringBuffer classes.

String

One important characteristic of the Java String class is that the string object cannot be changed. Even though the String class contains methods such as replace and toUpperCase, the object remains unchanged. Instead, these methods return a new String object. For example, the toLowerCase method returns a new String object with all of the characters in lower case. As a guideline, use Java String for string objects that will not change.

StringBuilder

The Java StringBuilder class represents string objects that can be changed. It contains methods such as append and insert that modify the string object. For example, the append method adds a string to the end of the StringBuilder object. The object is changed; no new object is created or returned. Generally use the StringBuilder class for string objects that will change.

StringBuffer

The Java StringBuffer class is just like StringBuilder. The primary difference is that StringBuffer is synchronized. In other words, multiple threads can safely process StringBuffer objects. However, additional overhead is required to make StringBuffer synchronized. So StringBuffer is slower than StringBuilder. You should use the StringBuffer class for string objects that will be changed by multiple threads of execution.

Choose your string class wisely

Choosing the wrong string class can cause undesirable results. For example, you can easily append the letter X to a String object: String s = s + "X". But this concatenation operation inside a loop is inefficient. That is because the String class happens to create a StringBuilder (or StringBuffer) object to do the concatenation work. Creating this object every time is costly.

A better choice for this example is the StringBuilder class. For example, we can simply append the letter X to the StringBuilder object: sbd.append('X'). This is much faster than the previous example. In sample testing (200,000 iterations), the previous example took almost one minute to complete while StringBuilder took less than a second.

If you add a requirement for multiple threads, then StringBuffer becomes the better choice. But in sample testing (one million iterations), StringBuffer was five times slower than StringBuilder. However StringBuffer guarantees thread safety, whereas StringBuilder does not. In other words, you can get unpredictable results when using StringBuilder objects in multithreaded programs.

Conclusion

As you can see, the Java String, StringBuilder and StringBuffer classes are not interchangeable. Pick the string class that addresses your needs, or else your programs will be inefficient or even incorrect.

For more articles, please subscribe, add or follow me here.

Comments