0% found this document useful (0 votes)
17 views2 pages

String Buffer

StringBuffer is a mutable, thread-safe class in Java used for string manipulation, ideal for multi-threaded environments. It provides various methods for creating, modifying, and converting strings, along with managing capacity and length. In contrast to StringBuilder, StringBuffer is synchronized, making it slower but safer for concurrent access.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

String Buffer

StringBuffer is a mutable, thread-safe class in Java used for string manipulation, ideal for multi-threaded environments. It provides various methods for creating, modifying, and converting strings, along with managing capacity and length. In contrast to StringBuilder, StringBuffer is synchronized, making it slower but safer for concurrent access.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java StringBuffer Cheat Sheet

Note:
- StringBuffer is mutable like StringBuilder
- It is thread-safe (synchronized), but slightly slower than StringBuilder
- Use when multiple threads might access the same buffer

--------------------------------------------------

Creating a StringBuffer

StringBuffer sb1 = new StringBuffer(); // Empty


StringBuffer sb2 = new StringBuffer("Hello"); // With initial string
StringBuffer sb3 = new StringBuffer(50); // With initial capacity

--------------------------------------------------

Common Methods

sb.append("World"); // Add text at the end


sb.insert(5, " Java "); // Insert at index
sb.delete(5, 10); // Delete characters from index 5 to 9
sb.deleteCharAt(3); // Delete one character at index 3
sb.replace(0, 5, "Hi"); // Replace characters from 0 to 4
sb.reverse(); // Reverse the characters
sb.setCharAt(0, 'h'); // Set character at specific index
sb.setLength(3); // Truncate or pad to length 3

--------------------------------------------------

Capacity and Length

sb.length(); // Number of characters


sb.capacity(); // Buffer capacity
sb.ensureCapacity(100); // Ensure at least this capacity
sb.trimToSize(); // Trim to current length

--------------------------------------------------

Conversion

String str = sb.toString(); // Convert to String

--------------------------------------------------

Iteration Example

for (int i = 0; i < sb.length(); i++) {


System.out.println(sb.charAt(i));
}

--------------------------------------------------

Sample Code

StringBuffer sb = new StringBuffer("Hello");


sb.append(" World");
sb.insert(5, ",");
sb.replace(0, 5, "Hi");
System.out.println(sb.toString()); // Output: Hi, World

--------------------------------------------------

When to Use

- Use StringBuffer if your program is multi-threaded and you need safety


- If not dealing with threads, prefer StringBuilder for better performance

--------------------------------------------------

Difference from StringBuilder

StringBuffer is synchronized (thread-safe)


StringBuilder is not synchronized (faster in single-threaded use)

You might also like