Core Java

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 the StringBuilder class that constructs a string builder initialized to the contents of the specified string.
  • String(StringBuilder builder) – it is a constructor from the String 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 the StringBuilder class that returns a string representing the data in this sequence.
  • static String valueOf(Object obj) – it is a method in String class that returns the string representation of the Object 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.

convert string stringbuilder
Figure 1 StringBuilder Convertor

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 – converts String to StringBuilder via the StrngBuilder‘s constructor. Note: handles null as shown in line 16.
  • Line 22: stringToStringBuilder_Append – converts String to StringBuilder via the append method. It is used to build a string dynamically.
  • Line 31: StringBuilderToString – converts StringBuilder to String via the toString method. Note: handles null as shown in line 32.
  • Line 38: StringBuilderToString_valueOf – converts StringBuilder to String via the static 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 and StringBuilder 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.

Download
You can download the full source code of this example here: String and StringBuilder Converting Example

Mary Zheng

Mary graduated from the Mechanical Engineering department at ShangHai JiaoTong University. She also holds a Master degree in Computer Science from Webster University. During her studies she has been involved with a large number of projects ranging from programming and software engineering. She worked as a lead Software Engineer where she led and worked with others to design, implement, and monitor the software solution.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button