![]() |
![]() |
Java Interface | |
Java Class vs Interface | |
Java Multiple Inheritance | |
Java Nested Interface |
Java Packages | |
Java Built in packages | |
Java Package Access Specifiers |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
StringBuffer is a mutable sequence of characters. It is like a String bu the contents of the StringBuffer can be modified after creation.
StringBuffer strbuffer = new StringBuffer("Your String");
class SBuffer { public static void main(String args[]) { StringBuffer mystring = new StringBuffer("AndroidBerry"); System.out.println(mystring); } }
The capacity() method is used to get current capacity of the stringBuffer object. The default capacity of StringBuffer object is 16
characters. In above example While printing mystring.capacity() we get output as 28 (16 + 12). 12 is length of mystring.length() of 'AndroidBerry'
There are 3 different types of StringBuffer Constructors. 1 default and other 2 are parameterized.
This constructor creates an empty string of initial capacity of 16 characters
Example
StringBuffer sb = new StringBuffer();
This constructor creates an empty string with specified initial capacity of object. The value should not be less than 0
Example
StringBuffer sb = new StringBuffer(20);
This constructor creates an object having stringContent as argument. The capacity of this type of object is 16 + stringContent.length()
Example
StringBuffer sb = new StringBuffer("AndroidBerry");
All the methods are stored in the StringBuffer class of java.lang.StringBuffer.method()
Assume our StringBuffer objects are as shown below.
StringBuffer sb = new StringBuffer ("ABerry")
StringBuffer sb2 = new StringBuffer ("Android Berry")
StringBuffer sb3 = new StringBuffer ("Test")
sb3.append(123);
StringBuffer Method | Example | Used for | Output |
---|---|---|
sb.append (" Tutorials") | This method insertes the given string at the end of sb. | ABerry Tutorials |
sb.capacity () | This method returns the current capacity of sb object (length) + Initial capacity 16 | 22 |
sb.charAt (1) | This method returns specified character (starting from 0) from sb object. | B |
sb.delete (0,4) | This method removes the characters between first argument to second argument. | ry |
sb.deleteCharAt (0) | This method removes the specified character from given index. | Berry |
sb.ensureCapacity(12) | This method ensures that the capacity is at least equal to the specified minimum. | 34 |
sb2.indexOf("Berry") | This method returns starting index of Substring. | 34 |
sb.insert(sb.length()," Tutorials") | This method inserts the String or character at specified offset index | ABerry Tutorials |
sb.length() | This method counts the number of characters in the StringBuffer | 6 |
sb.reverse() | This method prints the String in reverse order. | yrreBA |
sb.setCharAt(0,'T') | This method replaces the single character from the given index | TBerry |
sb.substring(1) | This method replaces the single character from the given index | Berry |
sb3.toString() | This method returns a string representation of given data. | 123 |