🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuilder-class-in-java-with-examples
StringBuilder Class in Java - GeeksforGeeks
StringBuilder is a mutable sequence of characters provided by the java.lang package. Unlike String, its contents can be modified without creating a new object for every operation. It is commonly used when a string needs to be changed frequently, especially in single-threaded applications
Published: 1 week ago
Discussions

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
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
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
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
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 8 )
July 21, 2026 - StringIndexOutOfBoundsException - if the index is negative or greater than or equal to length(). public StringBuilder replace(int start, int end, String str)
🌐
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.
🌐
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 ...
🌐
Coderolls
coderolls.com › convert-stringbuilder-to-string-in-java
How To Convert StringBuilder To String In Java? | Coderolls
December 24, 2020 - /** * A Java program to convert StringBuilder to String * @author coderolls at cderlls.com * */ public class StringBuilderToString { public static void main(String[] args) { StringBuilder stringBuilder = new StringBuilder("Hello "); stringBuilder.append("Gaurav ") .append("Kukade!"); System.out.println("Printing strinBuilder as String: \n"); System.out.println(stringBuilder.toString()); } }
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-stringbuilder-tostring
Java StringBuilder toString()
October 4, 2022 - public class JavaExample { public ... {'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); } }...
🌐
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.
Find elsewhere
🌐
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.
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.

🌐
IONOS
ionos.com › digital guide › websites › web development › java stringbuilder
How to use Java’s StringBuilder - IONOS
January 3, 2025 - Java’s StringBuilder can be used to create mutable strings. We introduce the class to you and show you how to use it.
🌐
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.
🌐
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 article, we’ve explored how to convert between String and StringBuilder. By understanding these conversions, we can efficiently manipulate Strings in our Java programs.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 7 )
StringIndexOutOfBoundsException - if the index is negative or greater than or equal to length(). public StringBuilder replace(int start, int end, String str)
🌐
Medium
medium.com › @YodgorbekKomilo › mastering-javas-stringbuilder-the-ultimate-guide-for-beginners-8a579eb6d1f6
💡 Mastering Java’s StringBuilder: The Ultimate Guide for Beginners 🚀 | by Yodgorbek Komilov | Medium
May 9, 2025 - StringBuilder sb = new StringBuilder(); sb.append("Hello"); sb.append(" "); sb.append("World"); System.out.println(sb.toString()); // Output: Hello World ... StringBuilder sb = new StringBuilder("race"); sb.append("car"); System.out.println(sb); ...
🌐
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
🌐
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?