🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 8 )
July 21, 2026 - The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder.
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuilder-class-in-java-with-examples
StringBuilder Class in Java - GeeksforGeeks
The append() method adds the given string to the end of the existing sequence without creating a new object. This makes StringBuilder efficient for concatenation operations.
Published: September 1, 2026
People also ask

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 obj
🌐
simplilearn.com
simplilearn.com › home › resources › software development › stringbuilder in java: constructors, methods, and examples
Stringbuilder in Java: Constructors, Methods, and Examples [Updated]
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-thread
🌐
simplilearn.com
simplilearn.com › home › resources › software development › stringbuilder in java: constructors, methods, and examples
Stringbuilder in Java: Constructors, Methods, and Examples [Updated]
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 s
🌐
simplilearn.com
simplilearn.com › home › resources › software development › stringbuilder in java: constructors, methods, and examples
Stringbuilder in Java: Constructors, Methods, and Examples [Updated]
🌐
IONOS
ionos.com › digital guide › websites › web development › java stringbuilder
How to use Java’s StringBuilder - IONOS
January 3, 2025 - The insert() method is used in com­bi­na­tion with Java’s String­Builder to insert a string at a specific point. Here’s an example for how it works: public class Main { public static void main(String[] argv) throws Exception { StringBuilder str = new StringBuilder("ABCDE"); str.insert(1,"FGHIJK"); System.out.println(str); } }java ·
🌐
Simplilearn
simplilearn.com › home › resources › software development › stringbuilder in java: constructors, methods, and examples
Stringbuilder in Java: Constructors, Methods, and Examples [Updated]
July 8, 2026 - StringBuilder in Java is used to create mutable (modifiable) string. Learn all about it, important methods and constructors, and more now. Start learning!
Address: 5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Tutorialspoint
tutorialspoint.com › java › lang › java_lang_stringbuilder.htm
Java StringBuilder Class
package com.tutorialspoint; public class StringBuilderDemo { public static void main(String[] args) { StringBuilder stringBuilder = new StringBuilder("tuts "); System.out.println("builder = " + stringBuilder); // appends the boolean argument as string to the string stringBuilder stringBuilder.append(true); // print the string stringBuilder after appending System.out.println("After append = " + stringBuilder); stringBuilder = new StringBuilder("abcd "); System.out.println("stringBuilder = " + stringBuilder); // appends the boolean argument as string to the string stringBuilder stringBuilder.append(false); // print the string stringBuilder after appending System.out.println("After append = " + stringBuilder); } }
🌐
Great Learning
mygreatlearning.com › blog › it/software development › java stringbuilder class: methods, examples and more
Java StringBuilder Class: Methods, Examples and more
September 12, 2024 - The StringBuilder class doesn’t provide synchronization, and this is why Java StringBuilder is more suitable to work with than StringBuffer, as it works with a single thread. The StringBuilder class provides high performance compared with other string classes, and it is not thread-safe and provides other methods for various purposes.
🌐
Scaler
scaler.com › home › topics › java › stringbuilder in java with examples, methods and constructors
StringBuilder in Java with Examples, Methods, and Constructors - Scaler Topics
May 3, 2023 - The StringBuilder in Java is used for storing the mutable (changeable) sequence which means we can update the elements of the StringBuilder class without creating a new StringBuilder sequence in memory.
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuffer-and-stringbuilder-methods
StringBuffer and StringBuilder Methods - GeeksforGeeks
January 31, 2026 - In Java, strings are immutable, meaning their values cannot be changed once they are created. To support efficient string manipulation, Java provides mutable classes like StringBuilder and StringBuffer, which allow modifying strings without creating new objects.
Find elsewhere
🌐
Hyperskill
hyperskill.org › university › java › java-stringbuilder
Java StringBuilder
December 4, 2024 - Two methods should not be confused: length and capacity. The length returns the actual number of characters whereas capacity returns the amount of storage available for newly inserted characters, beyond which a new memory allocation will occur. The capacity is a part of the internal representation of StringBuilder, and its value will dynamically change.
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-stringbuilder-class
Java StringBuilder Class With Examples
public class JavaExample { public static void main(String args[]) { StringBuilder sb = new StringBuilder("This is Text"); //insert "a Sample" string after "is" //index starts with 0 so the index of s is 6 sb.insert(7, " a Sample"); System.out.println(sb); } }
🌐
Pass4sure
pass4sure.com › blog › stringbuilder-in-java-complete-guide-to-constructors-methods-and-usage
StringBuilder in Java: Complete Guide to Constructors, Methods, and Usage – IT Exams Training – Pass4Sure
While append always adds content at the end of the sequence, the insert method allows you to place new content at any position within the existing character sequence. It takes two arguments: an integer index specifying where the insertion should begin and the content to insert, which can be any of the same data types that append accepts. All characters currently at or after that index are shifted to the right to make room for the new content. For example, if your StringBuilder contains the text “Java is powerful” and you call insert(8, “extremely “), the result becomes “Java is extremely powerful”. The original content after position 8 slides rightward, and the new text occupies the space created.
🌐
Oracle
docs.oracle.com › javase › tutorial › java › data › buffers.html
The StringBuilder Class (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)
The principal operations on a StringBuilder that are not available in String are the append() and insert() methods, which are overloaded so as to accept data of any type. Each converts its argument to a string and then appends or inserts the characters of that string to the character sequence ...
Top answer
1 of 2
9

You're on the right track by understanding the immutability of the String class.

Based on [1] and [2], here are some cases where each type of implementation is recommended:

1. Simple String Concatenation

String answer = firstPart + "." + secondPart;

This is syntactic sugar for

String answer = new StringBuilder(firstPart).append("."). append(secondPart).toString();

This is actually quite performant and is the recommended approach for simple string concatenation [1].

2. Stepwise Construction

String answer = firstPart;
answer += ".";
answer += secondPart;

Under the hood, this translates to

String answer = new StringBuilder(firstPart).toString(); 
answer = new StringBuilder(answer).append(".").toString(); 
answer = new StringBuilder(answer).append(secondPart).toString();

This creates a temporary StringBuilder and intermediate String objects which are inefficient [1]. Especially if the intermediate results are not used.

Use StringBuilder in this case.

3. For Loop Construction and Scaling For Larger Collections

String result = "";

for(int i = 0; i < numItems(); i++) 
  result += lineItem(i);

return result;

The above code is O(n^2), where n is number of strings. This is due to the immutability of the String class and due to the the fact that when concatenating two strings, the contents of both are copied [2].

So it may be fine for a few fixed length items, but it will not scale. In such cases, use StringBuilder.

StringBuilder sb = new StringBuilder(numItems() * LINE_SIZE);

for(int i = 0; i < numItems(); i++)
  sb.append(lineItem(i));

return b.toString();

This code is O(n) time, where n is number of items or strings. So as the number of strings gets larger, you will see the difference in performance [2].

This code pre-allocates an array in the initialization of StringBuilder, but even if a default size array is used, it will be significantly faster than the previous code for a large number of items [2].

Summary

Use string concatenation if you are concatenating only a few strings or if performance is not of importance (i.e. a demonstration/toy-application). Otherwise, use StringBuilder or consider processing the string as a character array [2].

References:

[1] Java Performance: The Definitive Guide by Scott Oaks: Link

[2] Effective Java 3rd Edition by Joshua Bloch: Link

2 of 2
5

You cannot change the original string because it is immutable therefore having String s = ""; every operation like

s += "something";

will create and reassign new object (probably it will also add a little bit of work for GC in near future). On he other hand modifying StringBuilder is (usually) not creating new object (indeed it is happening just once at the very end when calling toString() method on builder instance)

Because of this it is common to use StringBuilder when you are modifying string many many times (for example in some long loops).

Still it is common error to overuse StringBuilder - it may be example of premature optimization


Read also:

  • Is it better to reuse a StringBuilder in a loop?
🌐
Codecademy
codecademy.com › docs › java › stringbuilder
Java | StringBuilder | Codecademy
April 24, 2025 - This example demonstrates the main operations you can perform with StringBuilder including append, insert, replace, delete, and reverse methods.
🌐
Great Learning
mygreatlearning.com › blog › it/software development › stringbuilder in java: efficient string manipulation for enhanced performance
StringBuilder in Java: Efficient String Manipulation for Enhanced Performance
September 3, 2024 - StringBuilder offers a set of methods specifically designed for appending, inserting, deleting, and replacing characters in a string. These methods provide efficient ways to modify strings without the need for additional memory allocations or ...
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 7 )
The principal operations on a StringBuilder are the append and insert methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string builder.
🌐
ZetCode
zetcode.com › java › stringbuilder
Java StringBuilder - mutable Java strings with StringBuilder
StringBuilder is a mutable sequence of characters. StringBuilder is used when we want to modify Java strings in-place. StringBuffer is a thread-safe equivalent similar of StringBuilder. StringBuilder has methods such as append, insert, or replace that allow to modify strings.
🌐
Java Guides
javaguides.net › 2018 › 08 › java-stringbuilder-class-api-guide.html
Java StringBuilder: Basics, Methods, Examples, Performance Tips
August 4, 2023 - Conversion to String: StringBuilder can easily be converted to a standard string using the toString() method. Alternative to StringBuffer: StringBuilder is almost identical to StringBuffer, but it lacks synchronization, making it more efficient when thread safety is not required. Java Version: Introduced in Java 5 as an alternative to StringBuffer, providing a non-synchronized option.
🌐
Intellipaat
intellipaat.com › home › blog › what is stringbuilder in java? methods and examples
StringBuilder in Java: Constructors, Methods with Examples
August 12, 2026 - 12. Void trimToSize(): This method is used for trimming the capacity of the string object of the StringBuilder class. In this section, we will understand how the above-mentioned methods are practically used in Java.