Skip to main content

String Buffer Example in Java

3 min read Updated May 29, 2026
Share:
On this page (10sections)

Introduction

String Buffer is a classic Java console program that demonstrates the concept with complete source code and sample output. Strings are immutable objects in Java; the examples show comparison, searching and transformation.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

Definition

StringBuffer class is a mutable class unlike the String class which is immutable. Both the capacity and character string of a StringBuffer Class. StringBuffer can be changed dynamically.

Syntax

StringBuffer Variable_name = new StringBuffer("String_sequence");

String Buffer Example Program

public class StringBufferExample {
	public static void main(String[] args) {
		StringBuffer sb = new StringBuffer("She sells sea shells ");
		System.out.println("Given stringbuffer is: " + sb);
		System.out.println("length of stringbuffer is: " + sb.length() + ", capacity of stringbuffer is: " + sb.capacity());
		System.out.println("character at index 5 of the stringbuffer is: " + sb.charAt(5));
		System.out.println("codePointAt index 5 of the stringbuffer is: " + sb.codePointAt(5));
		System.out.println("appendind the stringbuffer: " + sb.append("on the sea shore"));
		System.out.println("substring of stringbuffer from index 10 to 20 is: " + sb.substring(10,20));
		System.out.println("reverse of the stringbuffer is: " + sb.reverse());
	}
}

Sample Output

Given stringbuffer is: She sells sea shells
length of stringbuffer is: 21, capacity of stringbuffer is: 37
character at index 5 of the stringbuffer is: e
codePointAt index 5 of the stringbuffer is: 101
appendind the stringbuffer: She sells sea shells on the sea shore
substring of stringbuffer from index 10 to 20 is: sea shells
reverse of the stringbuffer is: erohs aes eht no sllehs aes slles ehS

When to use

Use string manipulation when cleaning user input, parsing text files or formatting messages.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. StringBuffer sb = new StringBuffer("She sells sea shells "); updates a variable used in the calculation or output.

  3. A println / print call writes text to the console — part of the sample output below.

  4. A println / print call writes text to the console — part of the sample output below.

  5. A println / print call writes text to the console — part of the sample output below.

  6. A println / print call writes text to the console — part of the sample output below.

  7. A println / print call writes text to the console — part of the sample output below.

  8. A println / print call writes text to the console — part of the sample output below.

Best Practices

  • Use meaningful variable and class names that describe their purpose.
  • Compile and run the program locally — modify values to see how output changes.
  • Read compiler errors carefully; they usually point to the exact line to fix.

Common Mistakes

  • Copying code without understanding each line — practice by changing one statement at a time.
  • Mismatching the public class name and the .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the String Buffer program demonstrate?
It shows how to implement string buffer in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use string manipulation when cleaning user input, parsing text files or formatting messages.

Related Tutorials

Search tutorials