There's nothing different between line 6 and line 8.

PrintStream.println(Object) calls string.valueOf(Object) which calls the object's toString() method, and that gets printed.

System.out.println(s) and System.out.println(s.toString()) have the same output (unless s is null, in which case the latter throws an exception).

The reason you call s.toString() directly is to get the current "built" value from the StringBuilder as a string so you can pass it on to other code which expects a string. If the code you want to call takes a StringBuilder you could just pass s, but then the called code has the ability to modify your value (they can't with string because it is an immutable data type).

Answer from bartonjs on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ stringbuilder-tostring-method-in-java-with-examples
StringBuilder toString() method in Java with Examples - GeeksforGeeks
August 18, 2026 - The toString() method of the StringBuilder class is the inbuilt method used to return a string representing the data contained by StringBuilder Object.
Discussions

java - Why do we need to use toString method with StringBuilder? - Stack Overflow
It prints an object... by calling its toString method. 2017-10-12T15:30:05.357Z+00:00 ... Yes, I had that in my head, and then read you post cheers for the reminder. I upvoted you answer too, as it's better than mine. 2017-10-12T15:31:12.837Z+00:00 ... StringBuilder needs toString() to convert ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
java - Using StringBuilder instead of toString() - Stack Overflow
The comments show the toString() method originally but I want to change it to a string builder method. I have a couple of these but this is the simplest one. You can see what I have tried. Thank yo... 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
java - Does StringBuilder need .ToString()? - Stack Overflow
I have a StringBuilder like so: ... I then want to make this equal to a string variable. Do I do this like so: ... To be fair I asked because I was using a StreamWriter and noticed it worked with or without .ToString(), I didn't think that this might be an overload of the StreamWriter method More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ standard โ€บ base-types โ€บ stringbuilder
Using the StringBuilder Class in .NET - .NET | Microsoft Learn
You must convert the StringBuilder object to a String object before you can pass the string represented by the StringBuilder object to a method that has a String parameter or display it in the user interface. You do this conversion by calling the StringBuilder.ToString method.
๐ŸŒ
Dot Net Perls
dotnetperls.com โ€บ stringbuilder-tostring
C# - StringBuilder ToString: Get String - Dot Net Perls
It internally converts the character buffer of the StringBuilder into a string object. ToString does this without allocating memory or copying a lot of data in most cases.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.text.stringbuilder
StringBuilder Class (System.Text) | Microsoft Learn
Convert the StringBuilder instance to a String by calling the ToString method, then access the characters in the string.
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2022 โ€บ 10 โ€บ java-stringbuilder-tostring
Java StringBuilder toString()
October 4, 2022 - public class JavaExample { public static void main(String[] args) { StringBuilder sb = new StringBuilder(); //char array char[] ch = {'a','e','i','o','u'}; //append array to StringBuilder sb.append(ch); //convert StringBuilder to String String str = sb.toString(); System.out.println("String: "+str); } }
๐ŸŒ
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?

๐ŸŒ
Andrew Lock
andrewlock.net โ€บ a-deep-dive-on-stringbuilder-part-3-converting-chunks-to-a-string-with-tostring
Converting chunks to a string with ToString(): A deep dive on StringBuilder - Part 3
July 27, 2021 - Given by m_ChunkLength + m_ChunkOffset. Note that this will be less than or equal to Capacity, and is the length of the string you will get when calling ToString(). Once you've added all your partial strings to StringBuilder, you call ToString() to get the final string out.
๐ŸŒ
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 StringBuilderToString { public static void main(String args[]) { StringBuilder sb = new StringBuilder(); sb.append("Hello World"); String result = sb.toString(); System.out.println(result); } } Output: Hello World
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.

๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ java โ€บ stringbuilder โ€บ .tostring()
Java | StringBuilder | .toString() | Codecademy
August 22, 2022 - The .toString() method returns a String representation of the character sequence currently in a given StringBuilder.
๐ŸŒ
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.
๐ŸŒ
Code Maze
code-maze.com โ€บ home โ€บ stringbuilder in c#
StringBuilder in C# - Code Maze
March 22, 2024 - We can use a StringBuilder for any form of string manipulation. But, the StringBuilder does not return a string. Hence, to retrieve a string, we must use the ToString() method: