After knowing the differences between String, StringBuffer and StringBuilder, let us Convert StringBuffer to String and vice versa. It is very much required in coding. Convert StringBuffer to String A StringBuffer object can be converted to String using toString() method of Object class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
StringBuffer sb1 = new StringBuffer("hello"); StringBuffer sb2 = new StringBuffer("hello"); String str1 = sb1.toString(); String str2 = sb2.toString(); if(str1.euqals(str2)) { System.out.println(“sb1 and sb2 both are same”); } else { System.out.println(“sb1 and sb2 both are not same”); } |
In the above code, equals() method…