Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Java StringBuffer and StringBuilder classes


May 10, 2021 Java


Table of contents


Java StringBuffer and StringBuilder classes

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

In addition to the String Class, objects in the StringBuffer and StringBuilder classes can be modified multiple times without generating new unused objects.

The biggest difference between the StringBuilder class, which is proposed in Java 5, is that the StringBuilder method is either thread-safe (thread-safe or multithreaded access, using a locking mechanism that protects one thread when it accesses one of the class's data, and other threads cannot access it until the thread has read it. T here will be no data insoars or data contamination. T hread insecurity doesn't provide data access protection, and it's possible that multiple threads change the data one after another so that the data you get is dirty data.

Because StringBuilder has a speed advantage over StringBuffer, the StringBuilder class is recommended in most cases. H owever, you must use the StringBuffer class when your application requires thread safety.

Instance

public class Test{

    public static void main(String args[]){
       StringBuffer sBuffer = new StringBuffer(" test");
       sBuffer.append(" String Buffer");
       System.out.println(sBuffer);  
   }
}

The above examples compile and run as follows:

test String Buffer

StringBuffer method

Here are the main methods supported by the StringBuffer class:

Serial number Method description
1 public StringBuffer append(String s)
Append the specified string to this sequence of characters.
2 public StringBuffer reverse()
Replace this character sequence with its inverse form.
3 public delete(int start, int end)
Remove the characters from the substrings of this sequence.
4 public insert(int offset, int i)
Insert a string representation of the int parameter into this sequence.
5 replace(int start, int end, String str)
Replace the String the substrings of this sequence with characters in a given String.

The methods in the following list are similar to those of the String class:

Serial number Method description
1 int capacity()
Returns the current capacity.
2 char charAt(int index)
Returns the char value at the specified index in this sequence.
3 void ensureCapacity(int minimumCapacity)
Make sure that the capacity is at least equal to the specified minimum.
4 void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin)
Copy characters from this sequence to the target character array dst
5 int indexOf(String str)
Returns the index of the specified substring that first appears in the string.
6 int indexOf(String str, int fromIndex)
Starting at the specified index, returns the index of the specified substring that first appears in the string.
7 int lastIndexOf(String str)
Returns the index of the specified substring that appears on the far right in this string.
8 int lastIndexOf(String str, int fromIndex)
Returns the index of the specified substring that last appeared in this string.
9 int length()
Returns the length (number of characters).
10 void setCharAt(int index, char ch)
Set the character at a given index to ch
11 void setLength(int newLength)
Sets the length of the character sequence.
12 CharSequence subSequence(int start, int end)
Returns a new sequence of characters, which are subsethics of this sequence.
13 String substring(int start)
Returns a new String contains the subsethic of characters currently contained in this character sequence.
14 String substring(int start, int end)
Returns a new String that contains the subsethic of characters currently contained in this sequence.
15 String toString()
Returns a string representation of the data in this sequence.