Menu
Topics Index
...
`

StringBufferConstructorDemo - Java Example Program


StringBufferConstructorDemo
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);    
    }
}
OUTPUT

Empty StringBuffer :
StringBuffer using CharSequence : ABC
StringBuffer using String : XYZ

DESCRIPTION

An empty StringBuffer is created at first. Next a StringBuffer is created using a CharSequence and finally a StringBuffer is created using a String.

THINGS TO TRY
  • Include the below code below LINE A
    CharSequence sequence = "ABC";
    System.out.println(cs.equals(sequence));
    The output will be true, since both references contain the same value.
  • Include the below line of code at LINE A.
    System.out.println(cs.equals(sb2));
    The output will be false even though both the references have the same value because there are references of two different types of objects.

© meritcampus 2019

All Rights Reserved.

Open In App