Java API (StringBuffer class and StringBuilder class)

String, StringBuffer and StringBuilder can all be used to define string variables, but there are differences.

  1. String - an immutable string, which is essentially a character array
  2. StringBuffer - variable string, inefficient, thread safe
  3. StringBuilder - variable string sequence, high efficiency, thread unsafe

1, StringBuffer class

Thread safe variable character sequence. A similar String String buffer, but cannot be modified. Although it contains a specific character sequence at any point in time, the length and content of the sequence can be changed through some method calls.

String buffers can be safely used for multiple threads. These methods can be synchronized when necessary, so all operations on any particular instance appear to occur in a serial order, which is consistent with the order of method calls made by each thread involved.

The main operations on StringBuffer are append and insert methods, which can be overloaded to accept any type of data. Each method can effectively convert the given data into a string, and then add or insert the characters of the string into the string buffer. The append method always adds these characters to the end of the buffer; The insert method adds characters at the specified point.

2, StringBuilder class

A variable character sequence. This class provides an API compatible with StringBuffer, but does not guarantee synchronization. This class is designed to be a simple alternative to StringBuffer when the string buffer is used by a single thread (which is common). If possible, it is recommended that this class be preferred because it is faster than StringBuffer in most implementations.

The main operations on StringBuilder are append and insert methods, which can be overloaded to accept any type of data. Each method can effectively convert the given data into a string, and then add or insert the characters of the string into the string generator. The append method always adds these characters to the end of the generator; The insert method adds characters at the specified point.

3, Common methods of StringBuffer class and StringBuilder class

StringBuffer() //Construct a string buffer without characters, with an initial capacity of 16 characters. 
StringBuffer(CharSequence seq) //Constructs a string buffer that contains the same characters as the specified CharSequence. 

StringBuilder() //Construct a string generator without characters, with an initial capacity of 16 characters. 
StringBuilder(CharSequence seq) //Constructs a string generator that contains the same characters as the specified CharSequence. 

public StringBuffer append(data type b)//Many append() methods are provided for string concatenation
public StringBuffer delete(int start,int end)//Delete the contents of the specified location
public StringBuffer insert(int offser,data type b)// Add a content at the specified location
public StringBuffer replace(int start,int end,String str)//Replace the contents of the specified range with other contents
public StringBuffer reverse()//String flip
package com.zking.test;

public class StringBufferAndStringBuilderDemo {
	public static void main(String[] args) {
		
		StringBuffer sb = "sb"; Cannot be defined directly
		StringBuffer sb = new StringBuffer("Who am I?");
		System.out.println(sb);
		
		StringBuffer sb = new StringBuffer("Who am I?");
		//Type of sb: StringBuffer type
		//Requirement: StringBuffer type -- String type
		String str = sb.toString();//Never remember the code ctrl+1 to the left of the symbol
		System.out.println(str);
		//Requirement: String type -- > StringBuffer type
		String name = "dengyankang";
		//You can convert directly through the constructor
		StringBuffer sb2 = new StringBuffer(name);
		
		//Different from String, there are the following methods
//		Append at the end of append
		StringBuffer sb = new StringBuffer("dengyankang");
		sb.append("liuxiangyang");
		System.out.println(sb);
		
//		insert adds the specified content at the specified index position
		sb.insert(2, "Jia Qing Wang");
		System.out.println(sb);
		
//		Delete delete
		sb.delete(2, 5);
		System.out.println(sb);
		
//		Replacement interval replacement
		sb.replace(0, 4, "tang");
		System.out.println(sb);
		
		
//		Reverse output
		sb.reverse();
		System.out.println(sb);
		
		StringBuilder sb2 = new StringBuilder();
	}
}

4, Differences among String, StringBuffer and stringbiolder

  1. String is immutable, while StringBuffer and StringBuilder are mutable classes.
  2. StringBuffer is thread safe and synchronous, while StringBuilder is not. This is why StringBuilder is faster than StringBuffer.
  3. The string concatenation operator (+) uses the StringBuilder class internally.
  4. For string operations in a non multithreaded environment, we should use StringBuilder, otherwise use the StringBuffer class.

Tags: Java Back-end

Posted on Sat, 04 Dec 2021 21:05:07 -0500 by jonno946