String Buffer Example in Java
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
-
Execution begins in the
mainmethod — the JVM calls this method when you run the class. -
StringBuffer sb = new StringBuffer("She sells sea shells ");updates a variable used in the calculation or output. -
A
println/printcall writes text to the console — part of the sample output below. -
A
println/printcall writes text to the console — part of the sample output below. -
A
println/printcall writes text to the console — part of the sample output below. -
A
println/printcall writes text to the console — part of the sample output below. -
A
println/printcall writes text to the console — part of the sample output below. -
A
println/printcall 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
.javafilename. - Forgetting semicolons at the end of statements.