What is Java StringBuilder and What its Purpose ?

As we know, the String class is responsible for providing support in string creation in Java.

Why StringBuilder is used?

A string is an immutable object and each time when we do some operation on a string, JVM automatically generates a new string object each time. This leads to memory wastage.

To overcome this problem, String Builder and String buffer are used.

A StringBuilder is used to create mutable or changeable string objects. It doesn’t guarantee thread safety like StringBuffer but provides it an advantage which is faster execution.

It supports methods to perform operations on the respective string object. It has two primary methods, append and insert.

Append adds a character sequence at the end each time, whereas the insert method puts a specific set of characters at a specified location. It automatically grows larger immediately when the internal buffer overflows.

However, when it comes to multi-threading where multiple threads try to access a string object, then one should go with StringBuffer.

Its immediate superclass is Object.

StringBuilder Constructors:

   // Default constructor calling
StringBuilder str = new StringBuilder();
 // Specifying Capacity
StringBuilder str1 = new StringBuilder(5);
// Using Sequence of character
 StringBuilder str2 =  new StringBuilder("Character sequence ");

Common Methods of StringBuilder Class:

These are some commonly used methods that are backed by StringBuilder class

append() :  It is used to append or concatenate another string at the end

capacity(): It is used to get the capacity

length(): It represents the actual size of the containing string

charAt(): It is used to figure out which element is at a specified position

delete(): It is used to remove a sequence of character from a specified range

deleteCharAt(): It deletes an element based on its position

indexOf(): It returns very first position or index-matched with specified string or character sequence, otherwise 0

 insert(): It is used to insert an element or character sequence

replace(): It replaces the existing characters that fall into a specified range with a new character sequence

reverse(): It reverses the string

Java program to demonstrate the working of StringBuilder and Its Methods?

Program:

package com.company;
public class Main {
    public static void main(String[] args) {
        StringBuilder str = new StringBuilder("Sandeep");
        int start = 4, end = 9;
        System.out.println("\n *****append()***** ");
// Concatenating str with "Verma"
        str.append(" Verma");
        System.out.println(str);
// To get the capacity of StringBuilder
        System.out.println("\n *****Capacity()***** ");
        System.out.println(str + " stored in an object with capacity : " + str.capacity());
        // length to get the size of the string
        System.out.println("\n *****length()***** ");
        System.out.println(" Actual Length : " + str.length());
// CharAt for figuring out which character is at specified index
        System.out.println("\n *****charAt()***** ");
        System.out.println("Character at 5 position : " + str.charAt(4));  // counting starts from zero
// delete for removing characters
        System.out.println("\n *****delete()***** ");
        str.delete(start, end);
        System.out.println(str);
// deleteCharAt for removing a character
        System.out.println("\n *****deleteCharAt()***** ");
        str.deleteCharAt(0);
        System.out.println(str);
// Getting index of very first character of matched string
        System.out.println("\n *****indexOf()***** ");
        System.out.println( " Position : "+str.indexOf("erma"));
//  inserting data at particular location
        System.out.println("\n *****insert()***** ");
        str.insert(5,"BIG");
        System.out.println(str);
 //replace a sequence of character with another
        System.out.println("\n *****replace()***** ");
 str.replace(start,end,"NEW STRING");
        System.out.println(str);
//reversing a string
        System.out.println("\n *****reverse()***** ");
        str.reverse();
        System.out.println(" Reversed String : "+str);
    }
}

Output:

 *****append()***** 
Sandeep Verma

 *****Capacity()***** 
Sandeep Verma stored in an object with capacity : 23

 *****length()***** 
 Actual Length : 13

 *****charAt()***** 
Character at 5 position : e

 *****delete()***** 
Sanderma

 *****deleteCharAt()***** 
anderma

 *****indexOf()***** 
 Position : 3

 *****insert()***** 
anderBIGma

 *****replace()***** 
andeNEW STRINGa

 *****reverse()***** 
 Reversed String : aGNIRTS WENedna

 

Leave a Reply