VOOZH about

URL: https://www.geeksforgeeks.org/java/stringbuffer-and-stringbuilder-methods/

⇱ StringBuffer and StringBuilder Methods - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

StringBuffer and StringBuilder Methods

Last Updated : 31 Jan, 2026

In Java, strings are immutable, meaning their values cannot be changed once they are created. To support efficient string manipulation, Java provides mutable classes like StringBuilder and StringBuffer, which allow modifying strings without creating new objects.

StringBuilder

StringBuilder is a mutable sequence of characters introduced in Java 1.5. It is not thread-safe, which makes it faster and suitable for single-threaded environments.


Output
Hello World

StringBuffer

StringBuffer is also a mutable sequence of characters, introduced in Java 1.0. Unlike StringBuilder, it is thread-safe because its methods are synchronized, making it suitable for multi-threaded environments.


Output
Java Programming

Common Methods of StringBuilder and StringBuffer

MethodOne-Line Description
append(String s)Appends the given string to the end of the current object.
insert(int offset, String s)Inserts a string at the specified position.
delete(int start, int end)Removes characters from the given range.
deleteCharAt(int index)Deletes the character at the specified index.
replace(int start, int end, String s)Replaces characters in the given range with a new string.
reverse()Reverses the sequence of characters.
length()Returns the number of characters in the string.
capacity()Returns the current allocated storage capacity.
charAt(int index)Returns the character at the specified index.
setCharAt(int index, char ch)Updates the character at the given index.
substring(int start)Extracts a substring starting from the given index.
substring(int start, int end)Extracts a substring within the specified range.
ensureCapacity(int minCapacity)Ensures that the buffer has at least the given capacity.

Note: StringBuilder and StringBuffer share the same methods, but they differ in performance and thread safety. StringBuilder is faster because it is not synchronized, while StringBuffer is slower as it is synchronized and thread-safe.

Comment
Article Tags:
Article Tags: