String Class is immutable (final modification ), Once a String After the object is created , The sequence of characters contained in this object is immutable , Until the object is destroyed .
public final class String implements, Comparable<String>,

Object represents a string with variable character sequence , Be a StringBuffer After being created , adopt StringBuffer Provided append(),insert(),reverse(),setCharAt(),setLength() You can change the character sequence of this string object . Once passed StringBuffer The final desired string is generated , You can call its toString() Method to convert it to a String object .

class , It also represents a string object . actually ,StringBuffer and StringBuilder Basically similar , The constructors and methods of the two classes are basically the same . The difference is that ,StringBuffer It's thread safe , and StringBuilder The thread safety function is not implemented , So the performance is slightly higher . So in general , If you need to create a variable content string object , Priority should be given to use StringBuilder class .

<> Execution speed comparison
public class JavaTest { public static void main(String[] args) { test1(); test2
(); test3(); } //String static void test1() { String str = null; Long start =
System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { str = str +
"hello world!"; } System.out.println(" time consuming :" + (System.currentTimeMillis() - start
)); } //StringBuffer static void test2() { StringBuffer sb = new StringBuffer();
Long start= System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { sb.
append("hello world!"); } System.out.println(" time consuming :" + (System.currentTimeMillis()
- start)); } //StringBuilder static void test3() { StringBuilder sb = new
StringBuilder(); Long start = System.currentTimeMillis(); for (int i = 0; i <
100000; i++) { sb.append("hello world!"); } System.out.println(" time consuming :" + (System.
currentTimeMillis() - start)); } }
console output :
time consuming :46029 time consuming :11 time consuming :9
The execution speed method of the three :StringBuilder > StringBuffer > String

Usage scenarios :

* If you want to operate a small amount of data, use ->String
* Single thread operation string buffer under the operation of a large number of data ->StringBuilder
* Multithreading operation string buffer under the operation of a large number of data ->StringBuffer

©2020 ioDraw All rights reserved
Interview questions : Handwritten list ( Include reverse linked list )C Notes on language learning —— Pointer : Pointer and one dimensional array error: (-215:Assertion failed) Solution layui.table Examples of dynamically getting header and list data python Simple record of network programming After the outbreak Which programming has a bright future Java Summary of basic learning (162)—— How to ensure thread safety ?vue el-input Do not enter special characters Only numbers can be entered Regular verification MYSQL database DML Common commands python in list and str Mutual conversion