Easy Tutorial
❮ Exception Thread Java String Contentequals ❯

Java StringBuffer and StringBuilder Classes

When modifying a string, you need to use the StringBuffer and StringBuilder classes.

Unlike the String class, objects of the StringBuffer and StringBuilder classes can be modified multiple times without creating new unused objects.

When using the StringBuffer class, each operation modifies the StringBuffer object itself rather than generating a new object. Therefore, if you need to modify a string, it is recommended to use StringBuffer.

The StringBuilder class was introduced in Java 5. The main difference between StringBuilder and StringBuffer is that the methods of StringBuilder are not thread-safe (cannot be accessed synchronously).

Due to the speed advantage of StringBuilder over StringBuffer, it is generally recommended to use the StringBuilder class in most cases.

Example

public class tutorialproTest {
    public static void main(String args[]) {
        StringBuilder sb = new StringBuilder(10);
        sb.append("tutorialpro..");
        System.out.println(sb);
        sb.append("!");
        System.out.println(sb);
        sb.insert(8, "Java");
        System.out.println(sb);
        sb.delete(5, 8);
        System.out.println(sb);
    }
}

The above example compiles and runs with the following results:

tutorialpro..
tutorialpro..!
tutorialpro..Java!
RunooJava!

However, in applications that require thread safety, you must use the StringBuffer class.

Test.java File Code:

public class Test {
    public static void main(String args[]) {
        StringBuffer sBuffer = new StringBuffer("tutorialpro.org official website: ");
        sBuffer.append("www");
        sBuffer.append(".tutorialpro");
        sBuffer.append(".com");
        System.out.println(sBuffer);
    }
}

The above example compiles and runs with the following results:

tutorialpro.org official website: www.tutorialpro.org

StringBuffer Methods

The following are the main methods supported by the StringBuffer class:

No. Method Description
1 public StringBuffer append(String s) <br>Appends the specified string to this character sequence.
2 public StringBuffer reverse() <br>Replaces this character sequence with its reversed form.
3 public delete(int start, int end) <br>Removes the characters in a substring of this sequence.
4 public insert(int offset, int i) <br>Inserts the string representation of the int argument into this sequence.
5 insert(int offset, String str) <br>Inserts the string into this sequence.
6 replace(int start, int end, String str) <br>Replaces the characters in a substring of this sequence with characters in the specified String.

The following list shows other commonly used methods of the StringBuffer class:

No. Method Description
1 int capacity() <br>Returns the current capacity.
2 char charAt(int index) <br>Returns the char value at the specified index.
3 void ensureCapacity(int minimumCapacity) <br>Ensures that the capacity is at least equal to the specified minimum.
4 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) <br>Copies characters from this sequence into the destination character array.
5 int indexOf(String str) <br>Returns the index of the first occurrence of the specified substring within this string.
6 int indexOf(String str, int fromIndex) <br>Returns the index of the first occurrence of the specified substring, starting from the specified index.
7 int lastIndexOf(String str) <br>Returns the index of the last occurrence of the specified substring within this string.
8 int lastIndexOf(String str, int fromIndex) <br>Returns the index of the last occurrence of the specified substring within this string, searching backwards from the specified index.
9 int length() <br>Returns the length (number of characters) of this string.
10 void setCharAt(int index, char ch) <br>Sets the character at the specified index to ch.
11 void setLength(int newLength) <br>Sets the length of the character sequence.
12 CharSequence subSequence(int start, int end) <br>Returns a new character sequence that is a subsequence of this sequence.
13 String substring(int start) <br>Returns a new String that contains a subsequence of characters currently contained in this character sequence.
14 String substring(int start, int end) <br>Returns a new String that contains a subsequence of characters currently contained in this sequence.
15 String toString() <br>Returns a string representation of the data in this sequence.
❮ Exception Thread Java String Contentequals ❯