YouTip LogoYouTip

Java Stringbuffer

\n\n\n\nJava StringBuffer and StringBuilder Classes\n\n\n

Java StringBuffer and StringBuilder Classes

\n

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

\n

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

\n

!(#)

\n

When using the StringBuffer class, operations are performed on the StringBuffer object itself, rather than generating a new object. Therefore, if you need to modify a string, it is recommended to use StringBuffer.

\n

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

\n

Since StringBuilder has a speed advantage over StringBuffer, it is recommended to use the StringBuilder class in most cases.

\n\n

Example

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

!(#)

\n

The compilation and execution results of the above example are as follows:

\n
Tutorial..\nTutorial..!\nTutorial..Java!\nRunooJava!\n
\n

However, in cases where thread safety is required by the application, the StringBuffer class must be used.

\n\n

Test.java File Code:

\n
public class Test{\n    public static void main(String[]args){\n        StringBuffer sBuffer = new StringBuffer("Official Website:");\n        sBuffer.append("www");\n        sBuffer.append(".tutorial");\n        sBuffer.append(".com");\n        System.out.println(sBuffer);\n    }\n}\n
\n

The compilation and execution results of the above example are as follows:

\n
Official Website: example.com\n
\n\n

StringBuffer Methods

\n

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

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

The following list shows other common methods of the StringBuffer class:

\n\n\n\n\n\n
No.Method Description
1int capacity()   Returns the current capacity.
2char charAt(int index)   Returns the char value at the specified index in this sequence.
3void ensureCapacity(int mi   Ensures that the capacity is at least equal to the specified minimum.
\n \n
← Plugins Mb MenubuttonJava String β†’