StringBuffer is a peer class of String that provides much of the functionality of strings. String represents fixed-length, immutable character sequences while StringBuffer represents growable and writable character sequences.
StringBuffer may have characters and substrings inserted in the middle or appended to the end. It will automatically grow to make room for such additions and often has more characters preallocated than are actually needed, to allow room for growth.
StringBuffer( ): It reserves room for 16 characters without reallocation.
StringBuffer s=new StringBuffer();
StringBuffer( int size)It accepts an integer argument that explicitly sets the size of the buffer.
StringBuffer s=new StringBuffer(20);
StringBuffer(String str): It accepts a String argument that sets the initial contents of the StringBuffer object and reserves room for 16 more characters without reallocation.
StringBuffer s=new StringBuffer("GeeksforGeeks");
Some of the most used methods are:
- length( ) and capacity( ): The length of a StringBuffer can be found by the length( ) method, while the total allocated capacity can be found by the capacity( ) method.
Length of string GeeksforGeeks=13 Capacity of string GeeksforGeeks=29
- append( ): It is used to add text at the end of the existence text. Here are a few of its forms:
StringBuffer append(String str) StringBuffer append(int num)
- insert( ): It is used to insert text at the specified index position. These are a few of its forms:
StringBuffer insert(int index, String str) StringBuffer insert(int index, char ch)
Here, index specifies the index at which point the string will be inserted into the invoking StringBuffer object.
GeeksforGeeks 5GeeksforGeeks 5GetrueeksforGeeks 5Getr41.35ueeksforGeeks 5Getr41.41.3535ueeksforGeeks 5Gpawanetr41.41.3535ueeksforGeeks
- reverse( ): It can reverse the characters within a StringBuffer object using reverse( ).This method returns the reversed object on which it was called.
- delete( ) and deleteCharAt( ): It can delete characters within a StringBuffer by using the methods delete( ) and deleteCharAt( ).The delete( ) method deletes a sequence of characters from the invoking object. Here, start Index specifies the index of the first character to remove, and end Index specifies an index one past the last character to remove. Thus, the substring deleted runs from start Index to endIndex–1. The resulting StringBuffer object is returned. The deleteCharAt( ) method deletes the character at the index specified by loc. It returns the resulting StringBuffer object.These methods are shown here:
StringBuffer delete(int startIndex, int endIndex) StringBuffer deleteCharAt(int loc)
- replace( ): It can replace one set of characters with another set inside a StringBuffer object by calling replace( ). The substring being replaced is specified by the indexes start Index and endIndex. Thus, the substring at start Index through endIndex–1 is replaced. The replacement string is passed in str.The resulting StringBuffer object is returned.Its signature is shown here:
StringBuffer replace(int startIndex, int endIndex, String str)
- ensureCapacity( ): It is used to increase the capacity of a StringBuffer object. The new capacity will be set to either the value we specify or twice the current capacity plus two (i.e. capacity+2), whichever is larger. Here, capacity specifies the size of the buffer.
void ensureCapacity(int capacity)
Besides that all the methods that are used in String class can also be used.
Some Interesting facts:
- java.lang.StringBuffer extends (or inherits from) Object class.
- All Implemented Interfaces of StringBuffer class:Serializable, Appendable, CharSequence.
- public final class StringBuffer extends Object implements Serializable, CharSequence, Appendable
- String buffers are safe for use by multiple threads. The methods can be synchronized wherever necessary so that all the operations on any particular instance behave as if they occur in some serial order.
- Whenever an operation occurs involving a source sequence (such as appending or inserting from a source sequence) this class synchronizes only on the string buffer performing the operation, not on the source.
- It inherits some of the methods from Object class which are clone, equals, finalize, getClass, hashCode, notify, notifyAll.
StringBuilder: J2SE 5 adds a new string class to Java’s already powerful string handling capabilities. This new class is called StringBuilder. It is identical to StringBuffer except for one important difference: it is not synchronized, which means that it is not thread-safe. The advantage of StringBuilder is faster performance. However, in cases in which you are using multithreading, you must use StringBuffer rather than StringBuilder.
This article is contributed by Lokesh Todwal. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- StringBuffer insert() in Java
- A Java Random and StringBuffer Puzzle
- String vs StringBuilder vs StringBuffer in Java
- StringBuffer setLength() in Java with Examples
- StringBuffer subSequence() in Java with Examples
- StringBuffer deleteCharAt() Method in Java with Examples
- StringBuffer substring() method in Java with Examples
- StringBuffer lastIndexOf() method in Java with Examples
- StringBuffer appendCodePoint() Method in Java with Examples
- StringBuffer replace() Method in Java with Examples
- StringBuffer reverse() Method in Java with Examples
- StringBuffer delete() Method in Java with Examples
- StringBuffer append() Method in Java with Examples
- StringBuffer toString() method in Java with Examples
- StringBuffer trimToSize() method in Java with Examples
Improved By : ShreyasWaghmare