What will be the output of the program?
public class Listing61
{
Listing61()
{
StringBuffer buf = new StringBuffer("log");
buf.append(":").append(1);
String s = buf.toString();
System.out.println(s + " " + s.length());
}
public static void main(String[] args)
{
new Listing61();
}
}
Answer: A
StringBuffer offers the same mutating API as StringBuilder, differing only in being synchronised, so the appends give log:1. That is five characters, and the int 1 is converted to its digit rather than added to anything. StringBuilder is preferred when no thread safety is needed.
Please log in or register to report this question.