String and StringBuilder

String is an immutable data structure. The characters are stored in an internal array. Once a String is created, you won’t be able to change the contents of the array.If one wants to update the string or build it in increments, StringBuilder is the answer. It holds similar data structure as String but is a mutable Object. Just like String, its contents are stored in a char array but one can manipulate it the way they want.Note that Java compilers substitutes expressions like A + B + C with new StringBuilder(A).append(B).append(C).toString() in order to avoid object creation performance penalties but it is still advisable to explicitly use StringBuilder as the compiler may not be able to do it the right way. For example, in case of concatenation inside a loop, the compiler may end up creating a StringBuilder for each iteration.

What is the difference between StringBuffer and StringBuilder

StringBuffer and StringBuilder, both extend the same data type AbstractStringBuilder so the actual logic that manipulates the char array is same.

They delegate the method calls to AbstractStringBuilder and AbstractStringBuilder holds the actual data structure which is nothing but a char array.

The only difference between StringBuffer and StringBuilder is that method in StringBuffer are synchronized. Use StringBuilder always to avoid the extra locking.

There are three constructors

  • Empty Constructor
  • String as parameter
  • Capacity as parameter
  • CharSequence as parameter which means any Object that implements CharSequence can become a suitable candidate for appending. For example, we can even append StringBuffer or StringBuilder itself.
Internally it constructs a char array with an initial capacity of 16 plus the length of the string passed in so basically it will always have 16 empty elements to be filled in unless you pass in the capacity itself in which case the array will be of the length of the capacity.
With each append, the char array is gradually filled in. Every time an append is called, it always makes sure that it has enough empty cells else it will opt for expansion of its internal array.
Note if we append a null string, string null will be appended.
    public void testNull() {
        String s = null;
        StringBuilder sb = new StringBuilder();
        assertEquals("null", sb.toString());

Does StringBuilder follows the builder design pattern?

In my opinion yes.
All the append methods that build the string returns the current Object itself so that one can conveniently call another build method on the returned StringBuilder. In the below example, the toString() calls various append methods to build the Object information.
public class StringBuilderPatternTest {
package patterns;
public class StringBuilderPatternTest {
    private String pattern;
    private String className;
    private StringBuilder externalSb;

    public StringBuilderPatternTest(StringBuilder sb, String designPattern) {
        className = getClass().getSimpleName();
        externalSb = sb;
        pattern = designPattern;

    public String toString() {
        StringBuilder sb = new StringBuilder();
                .insert(2, "H-el-lo", 1, 5);
        return sb.toString();

Test case:

    public void testSb() {
        StringBuilder sb = new StringBuilder("StringBuilder test:");
        StringBuilderPatternTest sbt = new StringBuilderPatternTest(sb, "Builder Pattern");


St-el-ringBuilder test:
Builder Pattern

Leave A Reply