Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Conversion from String to StringBuffer and StringBuilder

Here in this context We can directly pass String class object to StringBuffer and StringBuilder class constructors. As String class is immutable in java, so for editing a string, we can perform same by converting it to StringBuffer or StringBuilder class objects.

Example.java

public class Example {
	public static void main(String[] args) {
		String str = "SILAN";
		//Conversion from String object to StringBuffer
		StringBuffer sb1 = new StringBuffer(str);
		System.out.println(sb1);
		sb1.reverse();
		System.out.println(sb1);
		//Conversion from String object to StringBuilder
		StringBuilder sb2 = new StringBuilder(str);
		sb2.append("Software");
		System.out.println(sb2);
	}
}


Output:

SILAN

NALIS

SILANSoftware

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext