StringBuilder in Java is a class used to create a mutable, or in other words, a modifiable succession of characters. Like StringBuffer, the StringBuilder class is an alternative to the Java Strings Class, as the Strings class provides an immutable succession of characters. However, there is one significant difference between StringBuffer and StringBuilder, and it is that the latter is non-synchronized. It means that StringBuilder in Java is a more suited choice while working with a single thread, as it will be quicker than StringBuffer.

Class Declaration of StringBuilder

The java.lang.StringBuilder class is a part of java.lang package and has the following class declaration:

public final class StringBuilder

extends Object

implements Serializable, CharSequence

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Looking at the Constructors of StringBuilder in Java

The following table lists and describes the constructors of StringBuilder in Java

Constructor Name

Description

StringBuilder()

It constructs a blank string builder with a capacity of 16 characters

StringBuilder(int capacity)

It creates an empty string builder with the specified capacity

StringBuilder(CharSequence seq)

It creates a string builder with the same characters specified as the argument

StringBuilder(String str)

It will construct a string builder with the string specified in the argument

Since you now know about the constructors and class declaration of StringBuilder in Java, it’s time to look at an example where you will use some of these constructors to create various sequences of characters.

ConstructorsofStringBuilder

ConstructorsofStringBuilder_rslt

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Discussing Various Methods of StringBuilder in Java

The StringBuilder in Java provides numerous methods to perform different operations on the string builder. The tabledepicted below enlists some primary methods from the StringBuilder class.

Method

Description

StringBuilder append (String s)

This method appends the mentioned string with the existing string. You can also with arguments like boolean, char, int, double, float, etc.

StringBuilder insert (int offset, String s)

It will insert the mentioned string to the other string from the specified offset position. Like append, you can overload this method with arguments like (int, boolean), (int, int), (int, char), (int, double), (int, float), etc.

StringBuilder replace(int start, int end, String s)

It will replace the original string with the specified string from the start index till the end index.

StringBuilder delete(int start, int end)

This method will delete the string from the mentioned start index till the end index.

StringBuilder reverse()

It will reverse the string.

int capacity()

This will show the current StringBuilder capacity.

void ensureCapacity(int min)

This method ensures that the StringBuilder capacity is at least equal to the mentioned minimum.

char charAt(int index)

It will return the character at the specified index.

int length()

This method is used to return the length (total characters) of the string.

String substring(int start)

Starting from the specified index till the end, this method will return the substring.

String substring(int start, int end)

It will return the substring from the start index till the end index.

int indexOf(String str)

This method will return the index where the first instance of the specified string occurs.

int lastIndexOf(String str)

It will return the index where the specified string occurs the last.

Void trimToSize()

It will attempt to reduce the size of the StringBuilder.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Using the Methods of StringBuilder in Java

Let’s have a look at examples of some examples of the StringBuilder methods.

Example 1: Applying the Append() Method of StringBuilder in Java

Here, you must concatenate three strings using the append() method in the below example.

StringBuilderEx1

StringBuilderEx1_rslt

Example 2: Inserting String With the Insert() Method

In this example, you will insert one string into another at the specified index.

StringBuilderEx2

StringBuilderEx2_rslt

Example 3: Using the Replace() Method of StringBuilder in Java

You will use the replace() method to edit Simplilearn, and insert Java from a specified start and end index.

StringBuilderEx3

StringBuilderEx3_rslt

Master Core Java 8 Concepts, Java Servlet & More!

Java Certification TrainingENROLL NOW
Master Core Java 8 Concepts, Java Servlet & More!

Example 4: Deleting a Substring From the Original String

The delete() method in the below example will delete some strings according to the specified indexes.

StringBuilderEx4.

StringBuilderEx4_rslt.

Example 5: Applying the Reverse() Method of StringBuilder in Java

You will reverse “Simplilearn” in the example below with the reverse() method.

StringBuilderEx5

StringBuilderEx5_rslt

Example 6: Looking at the Capacity() Method

You will find out the current capacity of a StringBuilder using the capacity() method. The default capacity is 16. When the number of characters exceeds 16, it increases the capacity to n*2+2, where n is the current capacity. Let’s look at the example.

/StringBuilderEx6

StringBuilderEx6_rsll

Example 7: Ensure Minimum Capacity With the Ensurecapacity() Method

In this example, you will use the ensureCapacity() method of StringBuilder in Java to ensure that there is the minimum capacity before proceeding with other operations.

StringBuilderEx7

/StringBuilderEx7_rslt.

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program
Want a Top Software Development Job? Start Here!

Example 8: Using the Length() Method of Stringbuilder in Java

In this example, you will use the length() method to find the total number of characters in a string.

StringBuilderEx8

StringBuilderEx8_rslt.

Example 9: Looking at the Charat() Method

Using the charAt() method, you will find the character present at the specified index in the string.

StringBuilderEx9.

StringBuilderEx9_rsllt.

Example 10: The Indexof() Method of StringBuilder in Java

Next, use the indexOf() method to find the index of the specified string from the original string.

StringBuilderEx10.

StringBuilderEx10_rslt

FAQs

1. What is the string builder in Java?

StringBuilder is a class in the Java API that provides a mutable sequence of characters. It is used for dynamic string manipulation, such as building strings from many smaller strings or appending new characters to an existing string. Additionally, it is more efficient than using the "+" operator for string concatenation, as this creates a unique string every time. 

And added to this, Java String builder is a powerful tool for constructing strings. The builder can build strings of any length, from a single character to many characters. The builder is also thread-safe to be used in multi-threaded applications.

2. What are the different constructors of StringBuilder?

There are four constructors for the Java StringBuilder class.

  • StringBuilder(): This constructor creates an empty string builder object with no characters inside it with an initial capacity of 16.
  • StringBuilder(CharSequence seq): This type of constructor creates a string builder object with the specified sequence of characters as the initial value.
  • StringBuilder(int capacity): This specific constructor creates an empty string builder object with the specified capacity as the initial capacity.
  • StringBuilder(String str): This string constructor creates a string builder object with the specified string as the initial value.

3. List out some Java String builder methods

The java StringBuilder class provides the following methods:

  • Append(): It appends the specified string to the end of the StringBuilder object.
  • Insert(): This function inserts the specified string at the fixed position in the StringBuilder object.
  • Delete(): It deletes the characters in the specified range from the StringBuilder object.
  • Delete chart(): This string deletes the character at the specified position from the StringBuilder object.
  • Reverse(): It reverses the characters in the StringBuilder object.
  • ToString(): This string method returns the string representation of the StringBuilder object.
  • SetLength(): It sets the character sequence's length, and the sequence is changed to a new length, either shorter or longer than the previous one.
  • Replace(): This specific string builder replaces a portion of this sequence with the specified string.

4. Can you elaborate on the examples of Java String builders? 

The following are some prime examples of java StringBuilder,

1. Appends:

StringBuilder sb = new StringBuilder();

2. Insert:

StringBuilder sb = new StringBuilder("Hello World!");

3. Replace:

StringBuilder sb = new StringBuilder("Hello World!");

sb.replace(6, 11, "Java");

4. Creating an empty StringBuilder:

StringBuilder sb = new StringBuilder();

5. Reversing Strings:

sb.reverse();

5. What are some of the benefits of StringBuilder?

  • Improved Performance: StringBuilder is faster than String buffer because it does not have any synchronization.
  • Increased Flexibility: StringBuilder allows for more flexibility in manipulating strings.
  • Easy to use: StringBuilder is simple and does not require any synchronization.
  • Reduced Memory Usage: StringBuilder uses less memory than StringBuffer because it does not need to store previous values.
  • Efficient Coding: StringBuilder is more efficient in coding because it requires fewer lines of code than StringBuffer.

Conclusion

In this StringBuilder for Java article, you have learned everything about StringBuilder. You have also looked into the constructors and methods of the StringBuilder class. For more such basic concepts, you can refer to Simplilearn’s Java Tutorial for Beginners guide. But if you want to go advanced, opt for the Full Stack Java Developer. The course offers a vast array of learning materials and applied learning, to help you grasp both basic and advanced concepts of Java programming.

Have any questions for us? Leave them in the comments section of this article. Our experts will get back to you on the same, right away!

Our Software Development Courses Duration And Fees

Software Development Course typically range from a few weeks to several months, with fees varying based on program and institution.

Program NameDurationFees
Caltech Coding Bootcamp

Cohort Starts: 16 Dec, 2024

6 Months$ 8,000
Automation Test Engineer Masters Program

Cohort Starts: 27 Nov, 2024

8 months$ 1,499
Full Stack Java Developer Masters Program

Cohort Starts: 18 Dec, 2024

7 months$ 1,449
Full Stack (MERN Stack) Developer Masters Program

Cohort Starts: 8 Jan, 2025

6 Months$ 1,449