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

Java stringbuffer and stringbuilder object conversion to string

We can convert a StringBuffer or StringBuilder object to String by using toString() method of Object class.

StringConversion.java

class StringConversion {
	public static void main(String[] args) {
		StringBuffer sb1 = new StringBuffer("Silan");
		StringBuilder sb2 = new StringBuilder("Technology");
		String s1 = sb1.toString();
		String s2 = sb2.toString();
		System.out.println(s1 + " " + s2);     
	}
}

Output:

img

Another Example:

StringConversion1.java

class StringConversion1 {
	public static void main(String[] args) {
		StringBuffer sb1 = new StringBuffer("Silan");
		StringBuffer sb2 = new StringBuffer("Silan");
		String s1 = sb1.toString();
		String s2 = sb2.toString();
		if (s1.equals(s2)); {
			System.out.println("s1 and s2 contents are equal");
		} else {
			System.out.println("s1 and s2 contents are not equal");
		}
	}
}

Output:

img

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