Last Updated : 26 Jul, 2025
The StringBuffer class in Java represents a sequence of characters that can be modified, which means we can change the content of the StringBuffer without creating a new object every time. It represents a mutable sequence of characters.
Features of StringBuffer ClassThe key features of StringBuffer class are listed below:
Example: Here is an example of using StringBuffer to concatenate strings:
Java
public class Geeks {
public static void main(String[] args){
// Creating StringBuffer
StringBuffer s = new StringBuffer();
// Adding elements in StringBuffer
s.append("Hello");
s.append(" ");
s.append("world");
// String with the StringBuffer value
String str = s.toString();
System.out.println(str);
}
}
StringBuffer Constructors of StringBuffer Class
Example:
Java
public class Geeks {
public static void main(String[] args) {
// 1. Using default constructor
StringBuffer sb1 = new StringBuffer();
sb1.append("Hello");
System.out.println("Default Constructor: " + sb1);
// 2. Using constructor with specified capacity
StringBuffer sb2 = new StringBuffer(50);
sb2.append("Java Programming");
System.out.println("With Capacity 50: " + sb2);
// 3. Using constructor with String
StringBuffer sb3 = new StringBuffer("Welcome");
sb3.append(" to Java");
System.out.println("With String: " + sb3);
}
}
Default Constructor: Hello With Capacity 50: Java Programming With String: Welcome to JavaImplementation of Java StringBuffer Method 1. append() Method
append() method concatenates the given argument with this string.
Example:
Java
import java.io.*;
class Geeks {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello ");
sb.append("Java"); // now original string is changed
System.out.println(sb);
}
}
2. insert() Method
insert() method inserts the given string with this string at the given position.
Example:
Java
import java.io.*;
class Geeks {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello ");
sb.insert(1, "Java");
// Now original string is changed
System.out.println(sb);
}
}
3. replace() Method
replace() method replaces the given string from the specified beginIndex and endIndex-1.
Example:
Java
import java.io.*;
class Geeks {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Hello");
sb.replace(1, 3, "Java");
System.out.println(sb);
}
}
4. delete() Method
delete() method is used to delete the string from the specified beginIndex to endIndex-1.
Example:
Java
import java.io.*;
class Geeks {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Hello");
sb.delete(1, 3);
System.out.println(sb);
}
}
5. reverse() Method
reverse() method of the StringBuffer class reverses the current string.
Example:
Java
import java.io.* ;
class Geeks {
public static void main(String args[]) {
StringBuffer sb = new StringBuffer("Hello");
sb.reverse();
System.out.println(sb);
}
}
6. capacity() Method
Example:
Java
import java.io.*;
class Geeks {
public static void main(String args[])
{
StringBuffer sb = new StringBuffer();
// default 16
System.out.println(sb.capacity());
sb.append("Hello");
// now 16
System.out.println(sb.capacity());
sb.append("java is my favourite language");
// (oldcapacity*2)+2
System.out.println(sb.capacity());
}
}
7. length()
This method return the number of character in given string.
Java
import java.io.*;
class Geeks {
public static void main(String[] args) {
// Creating and storing string by creating object of StringBuffer
StringBuffer s = new StringBuffer("GeeksforGeeks");
// Getting the length of the string
int p = s.length();
// Getting the capacity of the string
System.out.println("Length of string GeeksforGeeks=" + p);
}
}
Length of string GeeksforGeeks=13Advantages of using StringBuffer
The advanatages of StringBuffer class are listed below:
Disadvantage of StringBufferNote: Both String and StringBuffer objects are thread safe, but in different ways. On the other hand immutable objects like String are thread-safe because their state can not be modified once they are created.
The table below describes all the methods of the StringBuffer class
Methods
Description
Syntax
append() Used to add text at the end of the existing text.sb.append(String str);
length() The length of a StringBuffer can be found by the length( ) method.int len = sb.length();
capacity() the total allocated capacity can be found by the capacity( ) method.
int cap = sb.capacity();
char ch = sb.charAt(int index);
sb.delete(int start, int end);
sb.deleteCharAt(int index);
sb.ensureCapacity(int minimumCapacity);
sb.insert(int offset, String str);
sb.reverse();
sb.replace(int start, int end, String str);
Increases StringBuffer capacity to the specified value
void ensureCapacity(int capacity)
appendCodePoint(int codePoint)
Appends code point as a string to the sequence.
public StringBuffer appendCodePoint(int codePoint)
charAt(int index)
Returns the char at the specified index.
public char charAt(int index)
IntStream chars()
Returns a stream of int values from zero-extended chars in the sequence..
public IntStream chars()
Returns the character (Unicode code point) at the specified index.
public int codePointAt(int index)
Returns the Unicode code point before the given index.
public int codePointBefore(int index)
Returns the count of Unicode code points in the specified text range.
public int codePointCount(int beginIndex, int endIndex)
IntStream codePoints()
Returns a stream of code points from the sequence.
public IntStream codePoints()
Copies characters from the sequence into the destination array.
public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Returns the index of the first occurrence of the specified substring.
public int indexOf(String str)
public int indexOf(String str, int fromIndex)
Returns the index of the last occurrence of the specified substring.
public int lastIndexOf(String str)
public int lastIndexOf(String str, int fromIndex)
Returns the index offset by a specified number of code points from the given index..
public int offsetByCodePoints(int index, int codePointOffset)
In this method, the character at the specified index is set to ch.
public void setCharAt(int index, char ch)
This method sets the length of the character sequence.
public void setLength(int newLength)
Returns index offset by given code points from the specified index.
public CharSequence subSequence(int start, int end)
Returns a new String containing a subsequence of this character sequence.
public String substring(int start)
public String substring(int start,int end)
Returns a string representing the data in the sequence.
public String toString()
Attempts to minimize storage used by the character sequence.
public void trimToSize()
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4