StringBuffer Methods in Java W3Schools Tutorial and Examples

Robert Jul 13, 2026

Java's StringBuffer class is an essential tool for managing and manipulating strings dynamically. Unlike the String class, which is immutable, StringBuffer is mutable and can be altered after its creation without causing changes to other strings. W3Schools provides a comprehensive guide to working with Java's StringBuffer methods.

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

With StringBuffer, multiple threads can access and manipulate a string without causing conflicts or unexpected results. Since it implements the synchronization methods from the java.lang zego surprisingly, it's often used in multiple-threaded environments. Let's delve into some of its most important methods.

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

Constructing a StringBuffer

The first step in working with StringBuffer is creating an instance. You can do this in a variety of ways, depending on your needs and the initial content of your string.

Java String Methods
Java String Methods

1. Without Initial Content: If you don't have an initial string, you can create a StringBuffer with no content. This is done using the default constructor: `StringBuffer sb = new StringBuffer();`.

Appending Content

String Methods in Java Explained | Java Programming Basics
String Methods in Java Explained | Java Programming Basics

Once you have a StringBuffer instance, you often need to add or manipulate its content. The most common way to add content is by using the `append()` method. It can accept any type starting from Java version 5, except for `null`.

Example: `sb.append("Hello");`

Inserting Content

Stringbuffer vs String builder
Stringbuffer vs String builder

The `insert()` method lets you insert content at a specific position within the StringBuffer.

Example: `sb.insert(0, "Welcome to ");` will insert "Welcome to " at the 0th position (starting from the left).

Manipulating the StringBuffer

Java Strings Cheat Sheet ☕ | Master String Problems for Coding Interviews
Java Strings Cheat Sheet ☕ | Master String Problems for Coding Interviews

Once you've added content to your StringBuffer, you may need to manipulate it - perhaps by deleting some content, reversing the order of the characters, or replacing certain characters.

Deleting Content

Strings in Java Explained | Java Programming Basics for Beginners
Strings in Java Explained | Java Programming Basics for Beginners
Java Strings - Learn the Essential Methods with its Syntax! - TechVidvan
Java Strings - Learn the Essential Methods with its Syntax! - TechVidvan
Java String equals() Method
Java String equals() Method
Java Bootcamp London & UK – Part Time Java Developer Program
Java Bootcamp London & UK – Part Time Java Developer Program
Everything about String Vs String Builder Vs String Buffer
Everything about String Vs String Builder Vs String Buffer
Multithreading in Java Explained | Thread Life Cycle & Creation Methods
Multithreading in Java Explained | Thread Life Cycle & Creation Methods
Methods Of String Class In Java With Example
Methods Of String Class In Java With Example
a cell phone with an image of a programming program on the screen and text that reads,
a cell phone with an image of a programming program on the screen and text that reads,
Strings & String Methods 💻
Strings & String Methods 💻
Java Basics in 15 Minutes ☕ | Beginner-Friendly Java Crash Course
Java Basics in 15 Minutes ☕ | Beginner-Friendly Java Crash Course
Java script string method
Java script string method
Java Exception Handling Made Simple
Java Exception Handling Made Simple
All important string methods in JavaScript 2024
All important string methods in JavaScript 2024
iterating through elements in a string | Java
iterating through elements in a string | Java
🔊 Master Java Methods Like a Pro! 🚀
🔊 Master Java Methods Like a Pro! 🚀
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
Java Methods - A Complete Guide to Master the Concept
Java Methods - A Complete Guide to Master the Concept
𝐉𝐒 𝐒𝐓𝐑𝐈𝐍𝐆 𝐌𝐄𝐓𝐇𝐎𝐃.
𝐉𝐒 𝐒𝐓𝐑𝐈𝐍𝐆 𝐌𝐄𝐓𝐇𝐎𝐃.
an image of a set of strings in the same language as it appears to be
an image of a set of strings in the same language as it appears to be

The `delete()` method allows you to remove a section of the string. The `delete(int start, int end)` method removes the substring from the start index (start is inclusive while end is exclusive).

Example: `sb.delete(5, 10);` will remove the characters from index 5 to 9 (but not the 10th).

Reversing Content

The `reverse()` method reverses the sequence of the characters in the StringBuffer.

Example: `sb.reverse();` for "Hello, World!" will result in "!dlroW ,olleH".

Converting StringBuffer to String

Oftentimes, you'll need to convert the StringBuffer back to a String when you're done manipulating it. You can easily do this through the `toString()` method.

Using `toString()`

The `toString()` method converts this StringBuffer to a string and returns that string.

Example: `String str = sb.toString();` will convert the current state of the StringBuffer 'sb' to a String named `str`.

Remember to use the correct StringBuffer method for the task at hand, and always consider the impact of your changes on the overall project. Happy coding!