The
StringBuffer class is a thread-safe, mutable sequence of characters. Following are the important points about
StringBuffer
:
- A
StringBuffer
is like a String
, but can be modified.
- It contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls.
- They are safe for use by multiple threads.
- Every
StringBuffer
has a capacity.
Constructors:
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);
}
}
OUTPUTEmpty StringBuffer :
StringBuffer using CharSequence : ABC
StringBuffer using String : XYZ
DESCRIPTIONAn empty StringBuffer
is created at first. Next a StringBuffer
is created using a CharSequence
and finally a StringBuffer
is created using a String
.