In Java, String, StringBuilder, and StringBuffer are used to handle text data, but they differ in mutability and thread safety. Understanding how to convert between them is important for performance optimization and clean coding practices.
Convert String to StringBuffer and StringBuilder
A String is immutable, meaning its content cannot be changed after creation. To perform modifications (like appending or reversing), convert it into a mutable object, such as a StringBuffer or StringBuilder.
public class Geeks {
public static void main(String[] args){
String s = "Geeks";
// Convert String to StringBuffer
StringBuffer sbr = new StringBuffer(s);
// Reverse the string
sbr.reverse();
System.out.println(sbr);
// Convert String to StringBuilder
StringBuilder sbl = new StringBuilder(s);
// Append more text
sbl.append("forGeeks");
System.out.println(sbl);
}
}
Output
skeeG GeeksforGeeks
Explanation: The above example shows how to convert a String into StringBuffer and StringBuilder. We are reversing the String using StringBuffer and then adds more text using StringBuilder.
Convert StringBuffer or StringBuilder to String
To convert StringBuffer or StringBuilder to String, use the toString() method. This method creates a new String object containing the same sequence of characters. Changes to the original StringBuffer or StringBuilder do not affect the String.Â
public class Geeks {
public static void main(String[] args){
StringBuffer sbr = new StringBuffer("Geeks");
StringBuilder sbl = new StringBuilder("Hello");
// Convert StringBuffer to String
String s1 = sbr.toString();
System.out.println("StringBuffer to String: " + s1);
// Convert StringBuilder to String
String s2 = sbl.toString();
System.out.println("StringBuilder to String: " + s2);
// Modifying original StringBuffer
sbr.append("ForGeeks");
System.out.println(sbr);
System.out.println(s1);
}
}
Output
StringBuffer to String: Geeks StringBuilder to String: Hello GeeksForGeeks Geeks
Explanation: The above example shows how to convert StringBuffer and StringBuilder objects into String with the help of toString() method.
Convert StringBuffer to StringBuilder or vice-versa
There is no direct conversion between StringBuffer and StringBuilder. You can achieve this by first converting to a String and then to the other class using its constructor.
public class Geeks{
public static void main(String[] args){
StringBuffer sbr = new StringBuffer("Geek");
// Convert StringBuffer to String
String s = sbr.toString();
// Convert String to StringBuilder
StringBuilder sbl = new StringBuilder(s);
System.out.println(sbl);
}
}
Output
Geek
Explanation: The above example shows how to convert a StringBuffer into a String and then converting the same String into a StringBuilder.
Important points:
- String objects are immutable; StringBuffer and StringBuilder are mutable.
- StringBuffer is thread-safe (synchronized), whereas StringBuilder is faster but not thread-safe.
- For single-threaded programs, prefer StringBuilder.
- Conversions are mostly done via constructors or toString() method.