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

Answer from mukundvemuri on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 8 )
October 20, 2025 - 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
Final String: Welcoe eeks Java is awXsome! In the above program, we use different methods of the StringBuilder class to perform different string manipulation operations such as append(), insert(), reverse() and delete().
Published   November 19, 2025
Discussions

Why would you use a StringBuilder method over a String in Java? - Stack Overflow
What are the benefits of using a StringBuilder method over a String? Why not just amend the content within a String? I understand that a StringBuilder is mutable, but if you have to write more line... More on stackoverflow.com
🌐 stackoverflow.com
java - Why StringBuilder when there is String? - Stack Overflow
The StringBuilder is 100s of times faster than strings when appending. Here's an article with a Java script you can run to see it for yourself: blog.terresquall.com/2023/08/… ... String does not allow appending. Each method you invoke on a String creates a new object and returns it. More on stackoverflow.com
🌐 stackoverflow.com
[Java] What exactly is stringBuilder and why use it instead of a traditional String?
If I remember correctly, Java will do things in memory that you might not be aware of. String is an example of this, it will allocate additional memory because it creates a new string object when doing += in Java More on reddit.com
🌐 r/learnprogramming
5
1
January 6, 2021
Question about StringBuilder
When you create a String literal(anytime you do 'Hello world", even passing it to a function) it'll most likely be interned, so if you try to use that same string literal elsewhere it'll be reused. It's more clear to define to a constant/variable and reuse it if it'll truly be reused a lot though . More on reddit.com
🌐 r/javahelp
27
2
June 22, 2024
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]
🌐
Tutorialspoint
tutorialspoint.com › home › java/lang › java stringbuilder class
Java StringBuilder Class
September 1, 2008 - This class inherits methods from the following classes − ... The following example shows the usage of Java StringBuilder append(Boolean b) method. Here, we are instantiating a StringBuilder object "buff" with the string name "tuts". Then, we invoke the append() method using the "buff" object with a boolean argument "true".
🌐
Simplilearn
simplilearn.com › home › resources › software development › stringbuilder in java: constructors, methods, and examples
Stringbuilder in Java: Constructors, Methods, and Examples [Updated]
July 31, 2025 - 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
🌐
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 combination with Java’s StringBuilder to insert a string at a specific point.
Find elsewhere
🌐
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 - In this example, the ensureCapacity() method is used to ensure that the capacity of the string is minimum before we perform more operations on that string. It is essential to know the capacity of the string before performing any operation. ... Learn Java the right way! Our course teaches you essential programming skills, from coding basics to complex projects, setting you up for success in the tech industry. ... This is all about the Java StringBuilder class, where we have seen the methods that can be used in this class.
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?
🌐
W3Schools
w3schools.com › java › java_strings.asp
Java Strings
There are many string methods available in Java. ... The toUpperCase() method converts a string to upper case letters.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › StringBuilder.html
StringBuilder (Java SE 17 & JDK 17)
January 20, 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.
🌐
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 - This method returns the first index of the text if it is present in the StringBuilder sequence, otherwise returns -1. Let's understand how to search the text in the StringBuilder object using an example. ... The world is present in the StringBuilder sequence at the starting index 11 but the text "ScalerZ" is not present in the StringBuilder sequence that's why -1 will return. Use this Java Compiler to compile your code.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.text.stringbuilder
StringBuilder Class (System.Text) | Microsoft Learn
// Initialize the StringBuilder with "ABC". StringBuilder sb = new StringBuilder("ABC", 50); // Append three characters (D, E, and F) to the end of the StringBuilder. sb.Append(new char[] { 'D', 'E', 'F' }); // Append a format string to the ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.lang.stringbuilder
StringBuilder Class (Java.Lang) | Microsoft Learn
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 ...
🌐
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.
🌐
Oracle
docs.oracle.com › javase › 6 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 6)
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.
🌐
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 ...
🌐
Codefinity
codefinity.com › courses › v2 › 8204075c-f832-4cb9-88b1-4e24e74ebdcb › 0d8ac683-ca3e-4b9a-b780-d8305391b86f › 508cbff8-983a-46e7-8b76-da5ad5807636
Learn StringBuilder | String
StringBuilder provides a rich set of methods, with one of the most valuable and frequently used being append(String str). This method allows us to add a specified string to the existing content of a StringBuilder object.
🌐
TechVidvan
techvidvan.com › tutorials › stringbuilder-class-in-java
StringBuilder Class in Java with Examples - TechVidvan
July 22, 2024 - The StringBuilder class, however, differs from the StringBuffer class in terms of synchronization. ... StringBuilder append(X x): This method appends the string representation of the X type argument to the sequence.
🌐
Dremendo
dremendo.com › java-programming-tutorial › java-stringbuilder
StringBuilder in Java Programming | Dremendo
The charAt() method returns the character at the specified index. In a string builder object, the first character starts from index 0. import java.util.Scanner; public class StringBuilderMethod { public static void main(String args[]) { String s; int l; Scanner sc=new Scanner(System.in); System.out.println("Enter a string"); s=sc.nextLine(); StringBuilder sb=new StringBuilder(s); System.out.println("Character at index 3="+sb.charAt(3)); } }
🌐
W3Resource
w3resource.com › java-tutorial › stringbuffer-stringbuilder.php
StringBuffer / StringBuilder Class - w3resource
Java provides StringBuffer and StringBuilder classes for improved string manipulation operation. StringBuffer and StringBuilder are similar classes except StringBuffer has all methods synchronized while StringBuilder has non-synchronized methods.