String and StringBuilder Converting Example
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 theStringBuilder
class that constructs a string builder initialized to the contents of the specified string.String(StringBuilder builder
) – it is a constructor from theString
class that allocates a new string from the sequence of characters currently contained in the string builder argument.String
toString()
– it is a method in theStringBuilder
class that returns a string representing the data in this sequence.static String
valueOf(Object obj)
– it is a method inString
class that returns the string representation of theObject
argument.
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
– convertsString
toStringBuilder
via theStrngBuilder
‘s constructor. Note: handlesnull
as shown in line 16. - Line 22:
stringToStringBuilder_Append
– convertsString
toStringBuilder
via theappend
method. It is used to build a string dynamically. - Line 31:
StringBuilderToString
– convertsStringBuilder
toString
via thetoString
method. Note: handlesnull
as shown in line 32. - Line 38:
StringBuilderToString_valueOf
– convertsStringBuilder
toString
via thestatic valueOf
method.
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:
String
is immutable andStringBuilder
is mutable.String
is thread-safe because it is immutable and every modification creates a new object.StringBuilder
is 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