🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuilder-tostring-method-in-java-with-examples
StringBuilder toString() method in Java with Examples - GeeksforGeeks
August 18, 2026 - Explanation: The toString() method converts the contents of the StringBuilder into a String.
🌐
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
Discussions

java - How to convert StringBuilder[] to String[] - Stack Overflow
How can I call the same? I'm trying to avoid String[] here because it is immutable (I have some sensitive data which needs to be cleared after function call). Do I have to call toString for each StringBuilder object stored in array? Sample code snippet would really help as I'm new to Java. More on stackoverflow.com
🌐 stackoverflow.com
Why would you use a StringBuilder method over a String in Java? - Stack Overflow
String answer = firstPart; answer ... If Stringbuilder was combined with the class String and += and + operators should only add strings to a list in the String class. Only when the total String was needed (i.e. with the = operator) the total list of (sub)Strings in the String class were added together. Now Java has an unnecessary ... More on stackoverflow.com
🌐 stackoverflow.com
scala - What's the correct way to convert from StringBuilder to String? - Stack Overflow
But intensionally, the toString ... built string". The intensional definitions are different, but should not vary over time, whereas the extensional ones happen to currently coincide, but can in principle drift apart over time. ... @sarveshseri "Scala StringBuilder::result() just delegates to Java StringBuilder ... More on stackoverflow.com
🌐 stackoverflow.com
What happens when i use toString() method on a StringBuilder?
More-or-less. It does indeed copy the data from the StringBuilder (according to the documentation), so in theory the amount of work is proportional to the length of the string. However, looking at the source code, it uses a fairly low-level bulk copy operation under the hood. So is likely faster in practice than manually looping through a char array. More on reddit.com
🌐 r/learnjava
7
31
June 30, 2020
🌐
Tutorialspoint
tutorialspoint.com › java › lang › stringbuilder_tostring.htm
Java StringBuilder toString() Method
The Java StringBuilder toString() method is used to retrieve a string representing the data in a StringBuilder object. A new String object is allocated and initialized to contain the character sequence currently represented by this object.
🌐
Baeldung
baeldung.com › home › java › java string › how to convert string to stringbuilder and vice versa in java
How to Convert String to StringBuilder and Vice Versa in Java | Baeldung
April 7, 2025 - On the other hand, StringBuilder is a mutable sequence of characters, which allows us to modify the contents without creating new objects. Therefore, we often convert a String to a StringBuilder for manipulations and then convert the StringBuilder object back to a String to obtain the result.
🌐
Blogger
javahungry.blogspot.com › 2021 › 09 › stringbuilder-to-string-java.html
How to Convert a StringBuilder to a String in Java | Java Hungry
public class StringBuilderToString2 { public static void main(String args[]) { StringBuilder sb = new StringBuilder(); sb.append("Java Developer"); String result = new String(sb); System.out.println(result); } } Output: Java Developer That's all for today, please mention in the comments in case you have any questions related to how to convert a StringBuilder to a String in Java.
🌐
Codecademy
codecademy.com › docs › java › stringbuilder
Java | StringBuilder | Codecademy
April 24, 2025 - StringBuilder returns a reference to a mutable sequence of characters which can be converted to a String using the toString() method. StringBuffer is another mutable sequence class in Java that is similar to StringBuilder.
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-stringbuilder-tostring
Java StringBuilder toString()
October 4, 2022 - public class JavaExample { public ... b = true;//boolean sb.append(ch); sb.append(num); sb.append(b); //convert StringBuilder to String String str = sb.toString(); System.out.println("String: "+str); } }...
🌐
Java Code Geeks
javacodegeeks.com › home › core java
String and StringBuilder Converting Example - Java Code Geeks
October 7, 2024 - Line 38: StringBuilderToString_valueOf – converts StringBuilder to String via the static valueOf method. In this step, I will run the java application StringBuilderConvertor.
Find elsewhere
🌐
IONOS
ionos.com › digital guide › websites › web development › java stringbuilder
How to use Java’s StringBuilder - IONOS
January 3, 2025 - If you want to make changes, you’ll need to create and save a whole new object. This creates a lot of data waste and can lead to decreased per­for­mance. If you use String­Builder in Java instead, you can make changes to strings without creating a new one.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 8 )
July 21, 2026 - As long as the length of the character sequence contained in the string builder does not exceed the capacity, it is not necessary to allocate a new internal buffer. If the internal buffer overflows, it is automatically made larger. Instances of StringBuilder are not safe for use by multiple threads.
🌐
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
🌐
Dot Net Perls
dotnetperls.com › stringbuilder-java
Java - StringBuilder Examples - Dot Net Perls
Here we import java.lang.StringBuilder. We use StringBuilder in a for-loop. StringBuilder optimizes cases where many append() calls occur. Part 1 We create a StringBuilder. Then we use a for-loop and append 6 values (2 in each iteration) to the StringBuilder. Part 2 We convert the data into a String object.
🌐
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 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. Suppose you want to change the StringBuilder object to String because next operations need String an input instead of StringBuilder in a particular program.
🌐
GeeksforGeeks
geeksforgeeks.org › java › conversion-between-string-stringbuilder-and-stringbuffer
Conversion Between String, StringBuilder, and StringBuffer - GeeksforGeeks
February 20, 2026 - We are reversing the String using StringBuffer and then adds more text using StringBuilder. To convert StringBuffer or StringBuilder to String, use the toString() method. This method creates a new String object containing the same sequence of ...
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?
Top answer
1 of 2
6

The toString implementation currently just redirects to the result method anyway, so those two methods will behave in the same way. However, they express slightly different intent:

  • toString requests a textual representation of StringBuilders current state that is "concise but informative (and) that is easy for a person to read". So, theoretically, the (vague) specification of this method does not forbid abbreviating the result, or enhancing conciseness and readability in any other way.
  • result requests the actual constructed string. No different readings seem possible here.

Therefore, if you want to obtain the resulting string, use result to express your intent as clearly as possible.

In this way, the reader of your code won't have to wonder whether StringBuilder.toString might shorten something for the sake of "conciseness" when the string gets over 9000 kB long, or something like that.

The mkString is for something else entirely, it's mostly used for interspersing separators, as in "hello".mkString(",") == "h,e,l,l,o".

Some further links:

  • The paragraph with "hashcode in hexadecimal" describes the default. It is just documentation inherited from AnyRef, because the creator of StringBuilder didn't bother to provide more detailed documentation.
  • If you look into code, you'll see that toString is actually just delegating to result.
  • The documentation of StringBuilder also mentions result() in the introductory overview paragraph.

Just use result().

2 of 2
2

TL;DR; use result as stated in the docs.

toString MUST never be called in anything at all for another purpose other than a quick debug.

mkString is inherited from collections hierarchy and it will basically create another StringBuilder so is very inefficient.

🌐
Reddit
reddit.com › r/learnjava › what happens when i use tostring() method on a stringbuilder?
r/learnjava on Reddit: What happens when i use toString() method on a StringBuilder?
June 30, 2020 -

So let's say i have a code like this:-

StringBuilder str = new StringBuilder("hello");
System.out.println(str.toString());    

So is it like there are two arrays, StringBuilder and String and we loop StringBuilder and copy it's contents character by character into the String? What would be the time complexity of this process?