๐ŸŽ‰ Special Offer !    Code: GET300OFF    Flat โ‚น300 OFF on every Java Course
Grab Deal ๐Ÿš€

StringBuffer Class in Java  


Introduction
  • The StringBuffer class in Java represents a sequence of characters that can be modified after creation (mutable strings).
  • It was introduced in JDK 1.0 version.
  • StringBuffer class is present in java.lang package.
  • Syntax :-
    package java.lang;
    
    public final class StringBuffer extends AbstractStringBuilder implements Serializable, CharSequence
    {
        // Fields
        // Constructors
        // Methods
    }
  • Unlike 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
Methods of StringBuffer Class:
  • Below are some common and important methods of 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
Properties of StringBuffer Class:
  1. Mutable:
    • StringBuffer objects are mutable, meaning their content can be changed after creation without creating a new object.
  2. Stored in Heap Memory:
    • StringBuffer objects are stored in heap memory, unlike string literals which are stored in the String Constant Pool (SCP).
  3. Thread-Safe (Synchronized):
    • StringBuffer methods are synchronized, making it safe to use in multi-threaded environments.
  4. Dynamic Resizing:
    • StringBuffer automatically increases its capacity as more characters are added, improving performance during multiple modifications.
  5. Unicode Support:
    • StringBuffer stores text as UTF-16 encoded Unicode characters, supporting multiple languages and symbols.