A comprehensive Java project demonstrating various operations and methods of the StringBuffer class. This repository contains practical examples for learning how to manipulate mutable strings in Java.
StringBuffer is a thread-safe, mutable sequence of characters in Java. Unlike String, which is immutable, StringBuffer allows you to modify the content of the string. This project showcases the most common and useful operations provided by the StringBuffer class.
Key Features:
- Thread-safe string manipulation
- Mutable character sequences
- Efficient string concatenation and modification
- Dynamic capacity management
StringBuffer/
├── Stringbuffer/
│ ├── src/
│ │ ├── StringbufferAppend.java # append() method demonstration
│ │ ├── StringbufferCapacity.java # capacity() method demonstration
│ │ ├── StringbufferDelete.java # delete() method demonstration
│ │ ├── StringbufferInsert.java # insert() method demonstration
│ │ ├── StringbufferReplace.java # replace() method demonstration
│ │ └── StringbufferReverse.java # reverse() method demonstration
│ └── .classpath # Eclipse project configuration
└── README.md # This file
Appends the specified string to the existing content.
File: StringbufferAppend.java
class StringbufferAppend{
public static void main(String args[]){
StringBuffer sb = new StringBuffer("Hello ");
sb.append("Java"); // Append "Java" to the end
System.out.println(sb);
}
}Output:
Hello Java
Explanation: The append() method concatenates "Java" to the existing "Hello ", resulting in "Hello Java".
Returns the current capacity of the StringBuffer. The default capacity is 16.
File: StringbufferCapacity.java
public class StringbufferCapacity {
public static void main(String args[]){
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity()); // Default capacity
sb.append("Hello");
System.out.println(sb.capacity()); // Still 16
sb.append("Java is my Favourite Subject");
System.out.println(sb.capacity()); // Expanded capacity
}
}Output:
16
16
34
Explanation:
- Initial capacity: 16 (default)
- After appending "Hello" (5 chars): still 16 (enough space)
- After appending longer text: capacity expands to (16 × 2) + 2 = 34
Removes characters from the specified range (startIndex to endIndex, where endIndex is exclusive).
File: StringbufferDelete.java
public class StringbufferDelete {
public static void main(String args[]){
StringBuffer sb = new StringBuffer("University");
sb.delete(1, 3); // Remove characters at index 1-2
System.out.println(sb);
StringBuffer sb1 = new StringBuffer("University");
sb1.delete(4, 7); // Remove characters at index 4-6
System.out.println(sb1);
StringBuffer sb2 = new StringBuffer("University");
sb2.delete(3, 5); // Remove characters at index 3-4
System.out.println(sb2);
}
}Output:
Uiversity
Univty
Uniaersity
Explanation:
- Original: "University" (U=0, n=1, i=2, v=3, e=4, r=5, s=6, i=7, t=8, y=9)
- delete(1, 3): Removes 'n' and 'i' → "Uiversity"
- delete(4, 7): Removes 'e', 'r', 's' → "Univty"
- delete(3, 5): Removes 'v' and 'e' → "Uniaersity"
Inserts the specified string at the given index.
File: StringbufferInsert.java
public class StringbufferInsert {
public static void main(String args[]){
StringBuffer sb = new StringBuffer("Hello ");
sb.insert(1, "Java"); // Insert "Java" at index 1
System.out.println(sb);
}
}Output:
HJavaello
Explanation: Inserts "Java" after the first character 'H' (at index 1), resulting in "HJavaello".
Replaces the characters in a substring with the specified string.
File: StringbufferReplace.java
public class StringbufferReplace {
public static void main(String args[]){
StringBuffer sb = new StringBuffer("University");
sb.replace(1, 6, "Java"); // Replace index 1-5 with "Java"
System.out.println(sb);
}
}Output:
UJavaity
Explanation: Replaces characters from index 1 to 5 ("niver") with "Java", resulting in "UJavaity".
Reverses the character sequence.
File: StringbufferReverse.java
public class StringbufferReverse {
public static void main(String args[]){
StringBuffer sb = new StringBuffer("Saturday Sunday");
sb.reverse();
System.out.println(sb);
StringBuffer sb1 = new StringBuffer("Assignment");
sb1.reverse();
System.out.println(sb1);
StringBuffer sb2 = new StringBuffer("Hello! I am John.");
sb2.reverse();
System.out.println(sb2);
}
}Output:
yadnuS yrutaS
tnemngissA
.nhoJ ma I !olleH
Explanation: The reverse() method reverses the entire sequence of characters, including spaces and special characters.
- Open Eclipse
- File → Import → General → Existing Projects into Workspace
- Select the repository folder
- Right-click on any
.javafile - Run As → Java Application
cd Stringbuffer/src
# Compile all files
javac *.java
# Run individual programs
java StringbufferAppend
java StringbufferCapacity
java StringbufferDelete
java StringbufferInsert
java StringbufferReplace
java StringbufferReverse| Feature | String | StringBuffer |
|---|---|---|
| Mutability | Immutable | Mutable |
| Thread-Safe | Yes (immutable) | Yes (synchronized) |
| Performance | Slower for modifications | Faster for modifications |
| Memory | Creates new object each time | Modifies existing object |
| Use Case | Read-only operations | Frequent modifications |
StringBuffer allows modification of the string content without creating new objects, making it memory-efficient for string manipulation.
- Length: Number of actual characters in the StringBuffer
- Capacity: Size of the buffer that can hold characters before resizing
StringBuffer methods are synchronized, making it safe for use in multi-threaded environments. For single-threaded operations, consider using StringBuilder for better performance.
Most operations use 0-based indexing:
- Index 0 = First character
- Index n-1 = Last character
- Range operations are typically startIndex (inclusive) to endIndex (exclusive)
- Iterative String Building: Efficiently build strings in loops
- String Manipulation: Modify, insert, replace, or reverse strings
- Thread-Safe Operations: Use in multi-threaded applications
- Dynamic Content: Build strings where content changes frequently
- String: Immutable string class
- StringBuilder: Non-synchronized mutable string class (preferred for single-threaded applications)
This project is ideal for:
- Java beginners learning string manipulation
- Understanding the difference between String and StringBuffer
- Practicing object-oriented programming
- Preparing for Java interviews
- StringBuffer is thread-safe but has synchronization overhead
- For single-threaded code, use StringBuilder instead (faster)
- Use
StringBuffer.append()in loops instead ofStringconcatenation with+
Feel free to fork this repository and add more examples or improvements!
This project is open source and available under the MIT License.
Happy Learning! 🎓
For more information about StringBuffer, visit the Java Documentation.