Java StringBuffer Example: Learn with Code Snippets

Robert Jul 13, 2026

StringBuffer is a mutable sequence of characters in Java, and it's often used when we know we'll be manipulating the string rather than just creating it. It's part of the Java lang package and is more efficient for string manipulation than the String class. Here, we'll explore StringBuffer with examples.

StringBuffer Class in Java - Scientech Easy
StringBuffer Class in Java - Scientech Easy

StringBuffer is particularly useful when we need to append, insert, replace, or delete characters within a string dynamically. It offers a convenient way to build complex strings over time without having to repeatedly create new strings and waste memory.

Stringbuffer vs String builder
Stringbuffer vs String builder

Creating and Initializing StringBuffer

The StringBuffer class provides multiple constructors to create and initialize a StringBuffer object.

String vs StringBuffer vs StringBuilder | Java Comparison Table
String vs StringBuffer vs StringBuilder | Java Comparison Table

Let's consider an example of initializing a StringBuffer with a string:
```java StringBuffer sb = new StringBuffer("Hello"); ```

Appending to a StringBuffer

String vs StringBuffer vs StringBuilder in Java? Example
String vs StringBuffer vs StringBuilder in Java? Example

After creating a StringBuffer, we can append characters or strings to it using the append(...) method:

```java sb.append(", World!"); ```

Now, if we print the contents of sb, it will output "Hello, World!".

Difference Between String , StringBuilder and StringBuffer Classes  with Example : Java
Difference Between String , StringBuilder and StringBuffer Classes with Example : Java

Inserting into a StringBuffer

We can also insert characters or strings at a specific index in the StringBuffer using the insert(int offset, String str) method:

```java sb.insert(5, " there"); ```

Strings in Java | Difference Between String, StringBuffer & StringBuilder Explained
Strings in Java | Difference Between String, StringBuffer & StringBuilder Explained

This will insert " there" after the 'H' character, resulting in "Hellothere, World!".

StringBuffer Methods in Action

String Methods in Java Explained | Java Programming Basics
String Methods in Java Explained | Java Programming Basics
java for complete beginners - more on java strings
java for complete beginners - more on java strings
Java Strings - Learn the Essential Methods with its Syntax! - TechVidvan
Java Strings - Learn the Essential Methods with its Syntax! - TechVidvan
Multithreading in Java Explained | Thread Life Cycle & Creation Methods
Multithreading in Java Explained | Thread Life Cycle & Creation Methods
Java Bootcamp London & UK – Part Time Java Developer Program
Java Bootcamp London & UK – Part Time Java Developer Program
String, StringBuffer and StringBuilder in Java | Convert String to StringBuffer or StringBuilder and vice versa
String, StringBuffer and StringBuilder in Java | Convert String to StringBuffer or StringBuilder and vice versa
Java script string method
Java script string method
BufferedByteStream - Java | Geekboots
BufferedByteStream - Java | Geekboots
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Java Programming CheatsheetIntro to Java Programming: An Interdisciplinary Approach and Computer Science: An Interdisciplinary Approach by Sedgewick and Wayne
Methods Of String Class In Java With Example
Methods Of String Class In Java With Example
Java Strings Cheat Sheet ☕ | Master String Problems for Coding Interviews
Java Strings Cheat Sheet ☕ | Master String Problems for Coding Interviews
String concatenation using StringBuilder class
String concatenation using StringBuilder class
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
StringTokenizer in Java - Scientech Easy
StringTokenizer in Java - Scientech Easy
Strings in Java Explained | Java Programming Basics for Beginners
Strings in Java Explained | Java Programming Basics for Beginners
Java Data Types Explained
Java Data Types Explained
What are Arrays in Java?
What are Arrays in Java?
Java Variables Explained in 60 Seconds | Java Programming Basics
Java Variables Explained in 60 Seconds | Java Programming Basics

StringBuffer provides various methods to manipulate strings. Let's dive into some of these methods with examples:

1. replace(int start, int end, String str) - This method replaces the specified substring with the given new string: ```java sb.replace(7, 12, "you"); ```

2. delete(int start, int end) - This method deletes the substring from the start to the end index (both inclusive):

```java sb.delete(12, 17); ```

3. reverse() - This method reverses the entire sequence of characters in the StringBuffer:

```java sb.reverse(); ```

Finally, we can print the contents of our StringBuffer, which will now be "y Jet!dlroH ereht ouy".

StringBuffer is a powerful tool for dynamically building and manipulating strings in Java. Whether you're concatenating strings, appending or inserting substrings, replacing characters, or reversing the order of characters, StringBuffer is up to the task.

Explore more of StringBuffer's capabilities and keep honing your Java skills. Happy coding!