StringBuffer
class in Java represents a sequence of characters that can be modified after creation (mutable strings).
StringBuffer
class is present in java.lang
package.
package java.lang;
public final class StringBuffer extends AbstractStringBuilder implements Serializable, CharSequence
{
// Fields
// Constructors
// Methods
}
String
, StringBuffer objects are mutable, meaning you can change their content without creating a new object.
StringBuffer sb = new StringBuffer("Deepak");
sb.append(" Panwar"); // Modifies the same object
System.out.println(sb); // Output: Deepak Panwar
StringBuffer
Class:
StringBuffer
class with examples:
Method | Description | Example | Output |
---|---|---|---|
append(String str) |
Adds the specified string to the end of the current string. |
StringBuffer sb = new StringBuffer("Hello"); sb.append(" World"); System.out.println(sb); |
Hello World |
insert(int offset, String str) |
Inserts the specified string at the specified index. |
StringBuffer sb = new StringBuffer("Hello World"); sb.insert(6, "Java "); System.out.println(sb); |
Hello Java World |
replace(int start, int end, String str) |
Replaces characters from start index to end index with the specified string. |
StringBuffer sb = new StringBuffer("Hello World"); sb.replace(6, 11, "Java"); System.out.println(sb); |
Hello Java |
delete(int start, int end) |
Deletes characters from start index to end index. |
StringBuffer sb = new StringBuffer("Hello Java"); sb.delete(5, 10); System.out.println(sb); |
Hello |
reverse() |
Reverses the sequence of characters in the buffer. |
StringBuffer sb = new StringBuffer("Hello"); sb.reverse(); System.out.println(sb); |
olleH |
charAt(int index) |
Returns the character at the specified index. |
StringBuffer sb = new StringBuffer("Hello"); System.out.println(sb.charAt(1)); |
e |
setCharAt(int index, char ch) |
Replaces the character at the specified index with the specified character. |
StringBuffer sb = new StringBuffer("Hello"); sb.setCharAt(0, 'Y'); System.out.println(sb); |
Yello |
length() |
Returns the length (number of characters) of the buffer. |
StringBuffer sb = new StringBuffer("Hello"); System.out.println(sb.length()); |
5 |
capacity() |
Returns the current capacity of the buffer (total space allocated). |
StringBuffer sb = new StringBuffer("Hello"); System.out.println(sb.capacity()); |
Typically 21 (16 default + 5) |
setLength(int newLength) |
Sets the length of the buffer. If new length is greater, null characters are added; if smaller, characters are removed. |
StringBuffer sb = new StringBuffer("Hello World"); sb.setLength(5); System.out.println(sb); |
Hello |
substring(int start, int end) |
Returns a new string containing characters from start index to end index. |
StringBuffer sb = new StringBuffer("Hello World"); System.out.println(sb.substring(0, 5)); |
Hello |
StringBuffer
Class:
StringBuffer
objects are mutable, meaning their content can be changed after creation without creating a new object.
StringBuffer
objects are stored in heap memory, unlike string literals which are stored in the String Constant Pool (SCP).
StringBuffer
methods are synchronized, making it safe to use in multi-threaded environments.
StringBuffer
automatically increases its capacity as more characters are added, improving performance during multiple modifications.
StringBuffer
stores text as UTF-16 encoded Unicode characters, supporting multiple languages and symbols.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
Weโre here to make our tutorials better based on your thoughts and suggestions.