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.

Full Stack Java Developer Course

In Partnership with HIRIST and HackerEarthEXPLORE COURSE
Full Stack Java Developer Course

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

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.


Webinar: Why DSA Is Vital in Software Interviews

Thursday, 8 December | 8 PM ISTRegister Now
Webinar: Why DSA Is Vital in Software Interviews


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.

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.



Full Stack Web Developer Course

To become an expert in MEAN StackView Course
Full Stack Web Developer Course

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.



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.



New Course: Full Stack Development for Beginners

Learn Git Command, Angular, NodeJS, Maven & MoreEnroll Now
New Course: Full Stack Development for Beginners

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.



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.



Free Course: Programming Fundamentals

Learn the Basics of ProgrammingEnroll Now
Free Course: Programming Fundamentals

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.



Get a firm foundation in Java, the most commonly used programming language in software development with the Java Certification Training Course.


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 Online Java Certification Course. 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.
  • *According to Simplilearn survey conducted and subject to terms & conditions with Ernst & Young LLP (EY) as Process Advisors