StringBuffer() Class in Java
The Java StringBuffer Class is just like the String Class, but with the difference that the StringBuffer class is mutable or can be modified. Whereas, the String class cannot be modified, in other words, it is immutable.
The string that can be altered or changed or modified is known as a mutable string and the StringBuffer and StringBuilder classes are used to create mutable strings. As we know, String objects are immutable and if we want to make any changes or modifications with String objects, then we will end up with a memory leak. So, the StringBuffer() class allows us to make any modifications to our strings.
The following are some of the advantages of the StringBuffer() class.
- Can be changed dynamically.
- Is thread-safe, in other words, multiple threads cannot access it simultaneously. Hence, it is safe and will result in an order.
- Every StringBuffer has a capacity.
- It contains some specified character sequences that cannot be changed, but the length and content of the sequence can be changed by calling certain methods.
- Strings can be obtained from String buffers.
There are four constructors used by the StringBuffer() Class.
- StringBuffer()
- StringBuffer(String str)
- StringBuffer(int capacity)
- StringBuffer(CharSequence []ch)

StringBuffer() constructs an empty string buffer with an initial capacity of 16 characters.
StringBuffer(String str) constructs a string buffer with the same characters as specified in charSequence.
StringBuffer(int capacity) constructs an empty string buffer with specified initial capacity as length.
StringBuffer(CharSequence []ch) constructs a string buffer initialized to the contents of the specified string.


Santhakumar MunuswamyPosted May 13, 2015, 4:18 PM
Thanks for nice article
NitinPosted May 13, 2015, 12:07 PM
nice
Gopi ChandPosted May 13, 2015, 8:44 AM
Thanks to all :)
Sibeesh VenuPosted May 13, 2015, 8:23 AM
Good one
Gowtham RajamanickamPosted May 13, 2015, 8:15 AM
useful...