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().

Answer from Andrey Tyukin on Stack Overflow
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.

Discussions

Convert the string into stringBuilder in java - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. I have one method which returns the string but I want the data inside the method in StringBuilder and convert it into string then how can I do it? More on stackoverflow.com
🌐 stackoverflow.com
In Java, why use StringBuilder instead of Strings?
Did you read the JavaDoc for StringBuilder? More on reddit.com
🌐 r/learnprogramming
75
134
September 4, 2022
How to convert from string to stringbuilder? - Post.Byes
When having a string that is defined as a stringbuilder it can be converted to a string by this: StringbuilderText.ToString(); But what about the otherway round? Thanks Ole More on post.bytes.com
🌐 post.bytes.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
🌐
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.
🌐
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.
🌐
JetBrains
intellij-support.jetbrains.com › hc › en-us › community › posts › 115000093250-Convert-StringBuilder-to-String-concatenation-suggestion
Convert StringBuilder to String concatenation suggestion – IDEs Support (IntelliJ Platform) | JetBrains
February 22, 2017 - As some of the Strings are method parameters, the compiler can't pre-concatenate the strings at compile time, so it'll just use a StringBuilder under the bonnet to do the job, anyway.
Find elsewhere
🌐
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 - In this example, we pass the begin and end indexes to the substring() method to get the required part from the StringBuilder‘s character sequence. Next, let’s look at the opposite direction, how to convert String objects to a StringBuilder.
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.text.stringbuilder
StringBuilder Class (System.Text) | Microsoft Learn
You'll have to convert the StringBuilder object to a String for these operations, and this can negate the performance benefit from using StringBuilder. For more information, see the Search the text in a StringBuilder object section. Consider using the StringBuilder class under these conditions: When you expect your code to make an unknown number of changes to a string at design time (for example, when you use a loop to concatenate a random number of strings that contain user input).
🌐
Codefinity
codefinity.com › courses › v2 › 2bdaaf95-c96c-4691-a41a-1ca3053e8449 › 4cb9be11-4352-4d58-8a53-d5a2c54e53e0 › 12fb24fc-a533-42ee-9b4b-81240213695b
Learn StringBuilder | String
Inside the parentheses, we can also pass a string value, and our StringBuilder variable will hold that value: ... To print our value to the console, we need to use one of the StringBuilder methods. It's the toString() method, which converts ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuilder-class-in-java-with-examples
StringBuilder Class in Java - GeeksforGeeks
Explanation : This program demonstrates different constructors of the StringBuilder class and how to append data using the append() method. The StringBuilder class provides various methods for string manipulation.
Published: September 1, 2026
🌐
Blogger
javahungry.blogspot.com › 2022 › 02 › string-to-stringbuilder.html
2 ways to Convert String to StringBuilder in Java | Java Hungry
To convert String to StringBuilder in Java you can use a StringBuilder class constructor that accepts String.
🌐
Kotlin
kotlinlang.org › api › core › kotlin-stdlib › kotlin.text › -string-builder
StringBuilder | Core API – Kotlin Programming Language
Appends the string representation of the specified short value to this string builder and returns this instance. ... Appends all arguments to the given StringBuilder.
🌐
GeeksforGeeks
geeksforgeeks.org › c# › stringbuilder-tostring-method-in-c-sharp
StringBuilder.ToString Method in C# - GeeksforGeeks
July 11, 2025 - Syntax: public override string ToString (); Return Value: This method returns the String representing the data contained by StringBuilder Object.
🌐
Post.Byes
post.bytes.com › home › forum › topic › c sharp › how to convert from string to stringbuilder?
How to convert from string to stringbuilder? - Post.Byes
When having a string that is defined as a stringbuilder it can be converted to a string by this: StringbuilderText.ToString(); But what about the otherway round? Thanks Ole
🌐
TutorialsPoint
tutorialspoint.com › article › how-to-convert-string-to-stringbuilder-and-vice-versa-java
How to convert String to StringBuilder and vice versa Java?
July 2, 2020 - Java provides StringBuffer class as a replacement of Strings in places where there is a necessity to make a lot of modifications to Strings of characters. You can modify/manipulate the contents of a StringBuffer over and over again without leaving behind a lot of new unused objects. The StringBuilder class was introduced as of Java 5 and the main difference between the StringBuffer and StringBuilder is that StringBuilder’s methods are not thread safe (not synchronized).
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › standard › base-types › stringbuilder
Using the StringBuilder Class in .NET - .NET | Microsoft Learn
Learn how to use the StringBuilder class in .NET. Use this class to modify a string without creating a new object.
🌐
Reddit
reddit.com › r/learnprogramming › [java] what exactly is stringbuilder and why use it instead of a traditional string?
r/learnprogramming on Reddit: [Java] What exactly is stringBuilder and why use it instead of a traditional String?
January 6, 2021 -

Going through firecode.io and saw this solution (to replace spaces with a certain string)

public static String replace(String a, String b) {

    String ans = "";
    for (char c: a.toCharArray() ){
        if (c == ' '){ ans += b; }
        else { ans += c; }
    }
    return ans;

}

A user commented and said:

Use StringBuilder instead - it's more efficient! String does not allow appending. Each method you invoke on a String creates a new object and returns it. This is because String is immutable - it cannot change its internal state. On the other hand StringBuilder is mutable. When you call append(..) it alters the internal char array, rather than creating a new string object.

I'm curious about this and wanted to ask (I'm new to Java):

  • If a traditional 'String' is immutable, why am I able to use it in a "+=" operation to append a char at the end?

  • So when you use the "+=" operation, the computer instantiates a brand new String object each and every time you do it? So effectively I'm creating/destroying multiple String objects over and over again with every "+="?

  • Coming from C++, I see a lot of the "+=" operation when working with strings. Is it the same situation in C++ as it is expressed here in Java?