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

Basics to Advanced - Learn It All!

Caltech PGP Full Stack DevelopmentExplore Program
Basics to Advanced - Learn It All!

Looking at the Constructors of StringBuilder in Java

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

Constructor Name



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.



Front or Back-End Development? Learn It All!

Caltech Coding BootcampExplore Program
Front or Back-End Development? Learn It All!

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.



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.

Here's How to Land a Top Software Developer Job

Full Stack Development-MEANExplore Program
Here's How to Land a Top Software Developer Job

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.



Example 2: Inserting String With the Insert() Method

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



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.



Kickstart Your Coding Career

Free Webinar | 6 June, Tuesday | 9 PM ISTRegister Now
Kickstart Your Coding Career

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.



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

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



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.



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.



Create and Showcase Your Portfolio from Scratch!

Caltech PGP Full Stack DevelopmentExplore Program
Create and Showcase Your Portfolio from Scratch!

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.



Example 9: Looking at the Charat() Method

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



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.




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:


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.


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 Post Graduate Program in Full Stack Web Development. 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!

About the Author

Ravikiran A SRavikiran A S

Ravikiran A S works with Simplilearn as a Research Analyst. He an enthusiastic geek always in the hunt to learn the latest technologies. He is proficient with Java Programming Language, Big Data, and powerful Big Data Frameworks like Apache Hadoop and Apache Spark.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.