In Java, if the String value is fixed and there is no frequent change then we can use the String class but if a String value is not fixed and keeps changing then using String is not an efficient solution because for every change in String, a new object will be created which affects the performance of the system. To handle this scenario it is recommended to use StringBuffer, The main advantage of the StringBiffer class over the String class is all changes occur in the same existing object only.
What is String Buffer?
In Java StringBuffer is one of the core classes available in Java.lang
package, specially designed to handle mutable sequences of characters.
String Buffer Constructor
- 1st Constructor
StringBuffer sb = new StringBuffer();
This constructor creates an empty StringBuffer object with default initial capacity 16, if the StringBuffer object reaches its max capacity then a new StringBuffer object will be created with-
new capacity = current capacity *2
public class StringBufferExample {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity());
sb.append("this is string class with initial capacity 16");
System.out.println(sb.capacity());
}
}
- 2nd Constructor
public StringBuffer( int capacity )
This constructor creates a StringBuffer Object with a given capacity.
- 3rd Constructor
public StringBuffer ( @NotNull String str )
This constructor creats an equivalent StringBuffer object for the given String with – capacity = str. length + 16
public class StringBufferExample {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer("The Code Data");
System.out.println(sb.capacity());
}
}
Important Methods of String Buffer Class
public int length() //Gives used capacity of StringBuffer
public int capacity() // Gives total capacity of StringBuffer
public char charAt( int index ) //Return the character present at given index
public void setCharAt( int index,char ch )
//This method is used to replace the character avilable at given index with provided character
public StringBuffer append( int i ) ; //Appends the string representation of the int argument to this sequence.
public StringBuffer append( @Nullable String str ); //Appends the string representation of the String argument to this sequence.
public StringBuffer append( char c ); //Appends the string representation of the char argument to this sequence.
public StringBuffer append( long lng ); //Appends the string representation of the long argument to this sequence.
public StringBuffer append( boolean b ); //Appends the string representation of the boolean argument to the sequence.
public StringBuffer delete( int start,
int end ) // This method is used to delete character from start index to end -1 index
public StringBuffer reverse() // this method cause character sequence to be replaced by the reverse of the sequence.
public void ensureCapacity( int minimumCapacity ) // This metod ensures that the capacity is at least equal to the specified minimum.
public void trimToSize() // This method deallocated extra alloted free memory
4 thoughts on “String Buffer in Java”