Cover Image for StringBuffer class
212 views

StringBuffer class

The Java StringBuffer class is a part of the Java Standard Library and is used to create and manipulate mutable sequences of characters. Unlike the String class, which is immutable (meaning its content cannot be changed after creation), the StringBuffer class allows you to modify the characters in the sequence without creating a new object each time.

Here are some key features and methods of the StringBuffer class:

  1. Mutable Character Sequence: A StringBuffer represents a mutable sequence of characters. You can add, modify, or remove characters from the sequence.
  2. Thread-Safe Operations: StringBuffer is designed to be thread-safe, meaning it can be safely used in a multi-threaded environment. It achieves this thread safety through synchronization. However, this synchronization comes at a cost in terms of performance.
  3. Performance Considerations: While StringBuffer is thread-safe, it’s not always the most efficient choice for single-threaded applications. If you don’t need thread safety, you can use the StringBuilder class, which is similar to StringBuffer but lacks synchronization, making it faster.
  4. Constructors:
  • StringBuffer(): Creates an empty StringBuffer with a default capacity.
  • StringBuffer(int capacity): Creates an empty StringBuffer with the specified initial capacity.
  • StringBuffer(String str): Creates a StringBuffer initialized with the contents of the specified string.
  1. Modifying Operations:
  • append(data): Appends various data types to the end of the sequence.
  • insert(index, data): Inserts data at the specified position.
  • setCharAt(index, ch): Sets the character at the specified index.
  • delete(start, end): Deletes characters from the start index (inclusive) to the end index (exclusive).
  • deleteCharAt(index): Deletes the character at the specified index.
  • replace(start, end, str): Replaces the characters in the specified range with the contents of the provided string.
  1. Capacity Management:
  • capacity(): Returns the current capacity of the StringBuffer.
  • ensureCapacity(minimumCapacity): Ensures that the StringBuffer has at least the specified capacity.
  • trimToSize(): Reduces the capacity of the StringBuffer to its current length.
  1. String Conversion:
  • toString(): Converts the StringBuffer to a String.
  1. Other Operations:
  • length(): Returns the current length (number of characters) in the StringBuffer.
  • charAt(index): Returns the character at the specified index.
  • subSequence(start, end): Returns a subsequence from the start index (inclusive) to the end index (exclusive).

Here’s an example of using StringBuffer:

public class StringBufferExample {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Hello, ");
        sb.append("World!");
        System.out.println(sb); // Output: Hello, World!

        sb.insert(7, "Java ");
        System.out.println(sb); // Output: Hello, Java World!

        sb.replace(7, 11, "C++");
        System.out.println(sb); // Output: Hello, C++ World!

        sb.delete(7, 11);
        System.out.println(sb); // Output: Hello, World!
    }
}

The StringBuffer is used to build and modify a mutable sequence of characters. You can see how the append, insert, replace, and delete methods are used to manipulate the content.

YOU MAY ALSO LIKE...

The Tech Thunder

The Tech Thunder

The Tech Thunder


COMMENTS