CODE
class StringBufferConstructorDemo
{
public static void main(String arg[])
{
StringBuffer sb1 = new StringBuffer();
System.out.println("Empty StringBuffer : " + sb1);
CharSequence cs = "ABC"; // LINE A
StringBuffer sb2 = new StringBuffer(cs);
System.out.println("StringBuffer using CharSequence : " + sb2);
String s = "XYZ"; // LINE B
StringBuffer sb3 = new StringBuffer(s);
System.out.println("StringBuffer using String : " + sb3);
}
}
Empty StringBuffer :
StringBuffer using CharSequence : ABC
StringBuffer using String : XYZ
An empty StringBuffer
is created at first. Next a StringBuffer
is created using a CharSequence
and finally a StringBuffer
is created using a String
.
LINE A
CharSequence sequence = "ABC";
System.out.println(cs.equals(sequence));
true
, since both references contain the same value.
LINE A
.System.out.println(cs.equals(sb2));
false
even though both the references have the same value because there are references of two different types of objects.