StringBuffer Class
All Implemented Interfaces:
Serializable, Appendable, CharSequence
Declaration of StringBuffer:
public final class StringBuffer
extends Object
implements Serializable, CharSequence
This class belongs to java.lang; package. StringBuffer class is same as String Class, but it can be modified. StringBuffer class is safe in multi-threaded environment ,because StringBuffer class is synchronized and thread safe and all the methods of this class is also synchronized and thread safe ,so that all the operations on any particular instance behaves as if they occur in some order.
StringBuffer class has a buffer capacity, and it is not neccessary to allocate a new internal buffer array, as long as length of character sequence exceed the capacity of buffer. If the internal buffer overflows, it is automatically made larger.
Constructor Summary of StringBuffer:
Constructor
|
Description
|
StringBuffer()
|
Constructs a string buffer with no characters in it and an initial
capacity of 16 characters.
|
StringBuffer(CharSequence seq)
|
Constructs a string buffer that contains the same characters as the
specified CharSequence.
|
StringBuffer(int capacity)
|
Constructs a string buffer with no characters in it and the specified
initial capacity.
|
StringBuffer(String str)
|
Constructs a string buffer initialized to the contents of the
specified string.
|
Example of StringBuffer:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class StringBufferClassExample { public static void main(String[] args) { //StringBuffer with default Constructor StringBuffer sb = new StringBuffer(); sb.append("Java"); sb.append("-"); sb.append("Identifiers"); System.out.println(sb); //StringBuffer with String in Constructor StringBuffer sb1 = new StringBuffer("Java-Identifiers"); System.out.println(sb1); //StringBuffer with initial capacity in Constructor StringBuffer sb2 = new StringBuffer(10); sb2.append("Java"); sb2.append("-"); sb2.append("Identifiers"); System.out.println(sb2); } } |
Output:
Java-Identifiers
Java-Identifiers
Java-Identifiers
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.
This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Java I/O Tutorial

No comments:
Post a Comment