A Quick Guide to String Builder in Java

StringBuilder class

Java provides us StringBuilder class that represents a mutable collection of characters. This class was introduced as an alternative to the String class as the String class object cannot be modified. StringBuilder class is defined under the java.lang package.

The class hierarchy is given below:

The StringBuilder class is similar to the StringBuffer class as both can be used to make a mutable string but differ in terms of synchronization. The StringBuffer class promises synchronization whereas the StringBuilder class doesn’t guarantee synchronization.

The syntax of the StringBuilder class is the following:

public final class StringBuilder
    extends Object
    implements Serializable, CharSequence

Constructors provided under StringBuilder class

StringBuilder class provides the following constructors in it:

  1. StringBuilder(): This is similar to a default constructor having no parameters. This constructor creates a string builder having no characters in it. The created string builder object has an initial capacity of 16 characters.
  2. StringBuilder(int capacity): It is parameterized constructor that creates a string builder having no characters in it. In this case, the initial capacity is provided by the capacity argument.
  3. StringBuilder(CharSequence seq): This constructor creates a string builder object that contains the same set of characters as provided as an argument while calling the constructor(CharSequence).
  4. StringBuilder(String str): This constructor creates a string builder that is initialized with the characters of the specified string.

Let us consider an example below to understand the working of the StringBuilder class:

Source Code:

class HelloWorld {
    public static void main(String[] args) {
       
       
        // Creating an object using
        // StringBuilder() constructor
        StringBuilder myString1 = new StringBuilder();

        // Now we are appending a string to
        // this object
        myString1.append("Hello World!");

        // Print string
        System.out.println("\nString1 is " + myString1.toString());

        // Creating an object
        // using StringBuilder(CharSequence) constructor
        StringBuilder myString2
            = new StringBuilder("How are you?");

        // Print string
        System.out.println("String2 is " + myString2.toString());

        // Creating a StringBuilder object
        // using StringBuilder(capacity) constructor
        StringBuilder myString3 = new StringBuilder(10);

        // Print the string
        System.out.println("String3 capacity is equal to "
                          + myString3.capacity());

        // Creating a StringBuilder object
        // using StringBuilder(String) constructor
        StringBuilder myString4
            = new StringBuilder(myString2.toString());

        // Print the string
        System.out.println("String4 = " + myString4.toString());
    }
}

write your code here: Coding Playground

Output:

String builder class also contains inbuilt methods in it. Some of these methods are given below:

  1. StringBuilder append(str): This method is used to append the string representation of the str type argument at the end of the sequence.
  2. StringBuilder appendCodePoint(int codePoint): Using this method we can append the string representation of the codePoint argument to this sequence.
  3. int capacity(): This method can be used to get the capacity of an object.
  4. char charAt(int index): This method is used to get the character stored at the specified index of the sequence.
  5. int indexOf(): This method is used to get the index within this string of the very first occurrence of the substring passed to this method.
  6. StringBuilder reverse(): This method causes the current sequence of characters to be replaced by the reverse of the original sequence.

Now we will see a program demonstrating the working of these methods:

Source Code:

class HelloWorld {
    public static void main(String[] args) {
       
       
        // Create a StringBuilder object
        // using default constructor
        StringBuilder myString1
            = new StringBuilder();


        myString1.append("Board Infinity");
       
        // Print the string
        System.out.println("String = "
                          + myString1.toString());

        // Reverse the string
        StringBuilder reverseMyString1 = myString1.reverse();

        // print string
        System.out.println("Reverse String = "
                          + reverseMyString1.toString());

        // Append '@'(64) to the string
        myString1.appendCodePoint(64);

        // Print the modified String
        System.out.println("Modified StringBuilder = "
                          + myString1);

        // Get the capacity
        int capacity = myString1.capacity();

        // Print the result
        System.out.println("StringBuilder = " + myString1);
        System.out.println("Capacity of StringBuilder = "
                          + capacity);
    }
}

write your code here: Coding Playground

Output:

Conclusion

In this article, we started with introducing StringBuilder class in Java. We also learned about different types of constructors defined in this class. Later, we discussed different member functions defined under this class.