public class StringBuilderConstructors {
public static void main(String args[]) {
StringBuilder sb3 = new StringBuilder("hello");
System.out.printf("buffer3 = %s\n", sb3.toString()); //written in textbook
System.out.printf(sb3); //i tried
System.out.println("buffer3=" + sb3); // i tried
}}
o/p:
buffer3 = hello
hello
buffer3=hello
doubt:
The book says toString() gives the string representation an object. That is, according to the first line of System.out....What I want to know is why is that needed when the other two ways of System.out.. gives the same output? I know its used to give string representation of user defined objects but is it really needed for StringBuffer/StringBuilder as they are also string anyways?