1. Introduction
Java String class is from the java.lang package and represents a sequence of characters and it’s immutable. The StringBuilder class is also from the java.lang package but represents a mutable sequence of characters. StringBuilder is not synchronized, so it has better performance than StringBuffer(thread-safe). In this example, I will create a simple Java application with the following methods to convert String Stringbuilder, and vice versa.
StringBuilder(String str)– it is a constructor from theStringBuilderclass that constructs a string builder initialized to the contents of the specified string.String(StringBuilder builder) – it is a constructor from theStringclass that allocates a new string from the sequence of characters currently contained in the string builder argument.StringtoString()– it is a method in theStringBuilderclass that returns a string representing the data in this sequence.static StringvalueOf(Object obj)– it is a method inStringclass that returns the string representation of theObjectargument.
2. Setup
In this step, I will create a simple Java project in Eclipse. After the project is created, then right-click to create a new class as shown in the following screenshot.
3. Convert String Stringbuilder
In this step, I will create a StringBuilderConvertor.java class that includes four methods:
StringBuilderConvertor.java
public class StringBuilderConvertor {
public static void main(String[] args) {
String testString = "Hello from JCG-MaryZheng";
StringBuilder sb = stringToStringBuilder_Constructor(testString);
System.out.println("String to StringBuilder via Constructor: " + sb);
System.out.println("String to StringBuilder via Append: " + stringToStringBuilder_Append("Hello", " World"));
System.out.println("StringBuilder to String: " + StringBuilderToString(sb));
System.out.println("StringBuilder to String_valueOf: " + StringBuilderToString_valueOf(sb));
}
public static StringBuilder stringToStringBuilder_Constructor(final String stringTxt) {
if (stringTxt == null) {
return new StringBuilder();
}
return new StringBuilder(stringTxt);
}
public static StringBuilder stringToStringBuilder_Append(final String... stringTxt) {
StringBuilder sb = new StringBuilder();
for (String txt : stringTxt) {
sb.append(txt);
}
return sb;
}
public static String StringBuilderToString(final StringBuilder StringBuilder) {
if (StringBuilder == null) {
return null;
}
return StringBuilder.toString();
}
public static String StringBuilderToString_valueOf(final StringBuilder StringBuilder) {
return String.valueOf(StringBuilder);
}
}
- Line 15:
stringToStringBuilder_Constructor– convertsStringtoStringBuildervia theStrngBuilder‘s constructor. Note: handlesnullas shown in line 16. - Line 22:
stringToStringBuilder_Append– convertsStringtoStringBuildervia theappendmethod. It is used to build a string dynamically. - Line 31:
StringBuilderToString– convertsStringBuildertoStringvia thetoStringmethod. Note: handlesnullas shown in line 32. - Line 38:
StringBuilderToString_valueOf– convertsStringBuildertoStringvia thestatic valueOfmethod.
4. Demo Convert String StringBuilder
In this step, I will run the java application StringBuilderConvertor.
StringBuilderConvertor Output
String to StringBuilder via Constructor: Hello from JCG-MaryZheng String to StringBuilder via Append: Hello World StringBuilder to String: Hello from JCG-MaryZheng StringBuilder to String_valueOf: Hello from JCG-MaryZheng
5. Conclusion
In this example, I create a simple Java application that converts String to StringBuilder with both constructor and valueOf methods. It also converts StringBuilder to String with constructor and toString methods. In many cases, String and StringBuilder are similar. Here are major differences:
Stringis immutable andStringBuilderis mutable.Stringis thread-safe because it is immutable and every modification creates a new object.StringBuilderis preferred when working in a single-threaded application as it has better performance.
6. Download
This was an example of a Java project which converted String to StringBuilder and vice versa.
You can download the full source code of this example here: String and StringBuilder Converting Example
Thank you!
We will contact you soon.
Mary ZhengOctober 7th, 2024Last Updated: October 7th, 2024

This site uses Akismet to reduce spam. Learn how your comment data is processed.