To answer the question directly, you can use streams to convert your StringBuilder[] to String[] easily enough:

String[] arr = Arrays.stream(sbCmd).map(StringBuilder::toString).toArray(String[]::new);

...but:

I'm trying to avoid String[] here because it is immutable (I have some sensitive data which needs to be cleared after function call).

You can't avoid it. If you have a library function that takes a String array, then you have to provide a String array - and that obviously requires the use of Strings.

Your only other option is to fork the library and remove the use of strings. Note that this is likely to be a time-consuming, and tedious operation however - it's not nearly as simple as just making sure no string parameters are passed in (as the library function could easily call sb.toString() and obtain a string.)

The much more pragmatic approach would be not to worry about strings being left in memory, instead running your code in a secure, isolated environment, and letting the environment do its job.

Answer from Michael Berry on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › stringbuilder-tostring-method-in-java-with-examples
StringBuilder toString() method in Java with Examples - GeeksforGeeks
July 11, 2025 - Now we are done with discussing ... using to.String() method. Here we will create a StringBuilder class object then we will store the array of string as its object. Later on, we will create another string and will throw all elements in this. Lastly, we will print this string. ... // Java Program to Convert Array of Strings to A String // Using toString() method // Importing ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › StringBuilder.html
StringBuilder (Java Platform SE 8 )
April 21, 2026 - StringIndexOutOfBoundsException - if the index is negative or greater than or equal to length(). public StringBuilder replace(int start, int end, String str)
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
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
Convert the string into stringBuilder in java - Stack Overflow
Just concatenate all those strings, without any separators? That'll look really bad. --- "I tried to do it" Then show us what you tried, so we can help let you know what you did wrong. ... Save this answer. ... Show activity on this post. ... But as was said StringBuilder and StringBuffer are different. Take a look here for more information on StringBuilder and StringBuffer · https://docs.oracle.com/javase... More on stackoverflow.com
🌐 stackoverflow.com
In Java, why use StringBuilder instead of Strings?
Strings in Java are immutable. Meaning once you set their value you cannot change it. So what happens when add something to a string is that a new string object gets created and the old one gets thrown away. This can get expensive if you do it a lot. StringBuilder doesn't have this problem since it's mutable More on reddit.com
🌐 r/learnprogramming
75
134
September 4, 2022
🌐
TutorialsPoint
tutorialspoint.com › converting-a-stringbuilder-to-string-in-java
Java StringBuilder toString() Method
September 2, 2023 - This method returns a string representation of this sequence of characters. If the StringBuilder class object does not contain the null value, the toString() method represents the object in a string format.
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-stringbuilder-tostring
Java StringBuilder toString()
October 4, 2022 - It returns a string that is equivalent ... num =101; //int value sb.append(num); //append integer to sb System.out.println("Give Number: "+num); String str = sb.toString(); //convert sb to string System.out.println("String equivalent: "+str); } }...
🌐
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.
🌐
Blogger
javahungry.blogspot.com › 2021 › 09 › stringbuilder-to-string-java.html
How to Convert a StringBuilder to a String in Java | Java Hungry
Using Constructor Read Also: String Interview Questions and Answers · The easiest way to convert StringBuilder to String is by invoking the toString() method of the StringBuilder class. Below are the steps: a. Create an object of StringBuilder class b. Append the data to the StringBuilder ...
Find elsewhere
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.

🌐
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 ...
🌐
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.
🌐
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.
🌐
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   May 8, 2026
🌐
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)
🌐
The Knowledge Academy
theknowledgeacademy.com › blog › stringbuilder-java
StringBuilder in Java: Constructors, Methods, and Examples
January 1, 2009 - It accomplishes this by looping through each character in the input string, checking if it's a letter or a space, and then appending the appropriate character to the StringBuilder.
🌐
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?

🌐
JetBrains
jetbrains.com › help › inspectopedia › StringBufferReplaceableByString.html
'StringBuilder' can be replaced with 'String' | Inspectopedia Documentation
March 31, 2026 - StringBuilder result = new StringBuilder(); result.append("i = "); result.append(i); result.append(";"); return result.toString(); ... Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable ...
🌐
Coderanch
coderanch.com › t › 787475 › java › Clarifying-Purpose-StringBuilder-toString-Method
Clarifying the Purpose of StringBuilder's toString() Method (Java in General forum at Coderanch)
January 23, 2025 - The toString() method must be called on "StringBuilder" to convert it into a "String," as it does not return a String. It's fine printing the String in the console.