The difference and use of StringBuffer and StringBuilder

Table of contents

1 Introduction

2. The difference between String, StringBuffer and StringBuilder

 3. The use of StringBuffer and StringBuilder


I. Introduction

Both StringBuffer and StringBuilder classes provide methods for manipulating strings. At the same time, the difference between StringBuffer and StringBuilder is also a very common interview question in Java. This blog will introduce the difference between String, StringBuffer and StringBuilder and how to use StringBuffer and StringBuilder.

Two, the difference between String, StringBuffer and StringBuilder

When modifying strings, 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, the StringBuffer object itself will be operated every time instead of generating a new object, so if you need to modify the string, it is recommended to use StringBuffer.

The StringBuilder class was proposed in Java 5. The biggest difference between it and StringBuffer is that the methods of StringBuilder are not thread-safe (cannot be accessed synchronously) . It is used where a single thread uses a string buffer (typically)

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

3. The use of StringBuffer and StringBuilder

Commonly used methods:

serial number method description
1 public StringBuffer append(String s)
Appends the specified string to this character sequence.
2 public StringBuffer reverse()
 Replaces this character sequence with its reversed form.
3 public delete(int start, int end)
Removes characters in substrings of this sequence.
4 public delete(int start, int end)
Removes characters in substrings of this sequence.
5 public delete(int start, int end)
Removes characters in substrings of this sequence.
6 public delete(int start, int end)
Removes characters in substrings of this sequence.
7 int capacity()
returns the current capacity.
8 char charAt(int index)
Returns the value at the specified index in this sequence  char .
9 String substring(int start, int end)
returns a new  Stringsubsequence containing the characters currently contained in this sequence.
10 String toString()
returns a string representation of the data in this sequence.

 

Example: 


public class Test{
    public static void main(String args[]){
        StringBuilder sb = new StringBuilder(9);   //初始长度为9

        //将指定的字符串追加到此字符序列
        sb.append("Hello World");    
        System.out.println(sb);  
        sb.append("!");
        System.out.println(sb); 

        //将 str 参数的字符串插入此序列中
        sb.insert(12, "Java");
        System.out.println(sb); 

        //移除此序列的子字符串中的字符
        sb.delete(12,16);
        System.out.println(sb);  
    }
}

Running result display:

Hello World
Hello World!
Hello World!Java
Hello World!

 

 

Reference: https://www.runoob.com/java/java-stringbuffer.html

Guess you like

Origin blog.csdn.net/HouGOD/article/details/123513310