Episode 56-57: Use of StringBuilder and StringBuffer
Distinguish String, String Buffer, String Builder
StringBuilder variable string, thread is not safe, but efficient, recommended to use.
StringBuffer variable string, thread-safe, but inefficient, not recommended.
String is an immutable string, which should be used in real time and not in loops, because it will produce good useless strings.
Answers to the Interview Questions of String, String Buffer and String Builder
StringBuilder is efficient, thread insecure, StringBuffer inefficient and thread safe.
String is an immutable string and StringBuilder is a variable string. (Why is there such a difference, you can go deep into the source code to parse, such as priver final char value [] and other methods in the String class.)
If it is a simple statement that a string does not have too many subsequent operations, String Builder can be used. If the subsequent character wear is frequently added, deleted, or dynamically changing the length of the character wear in the loop, String Builder should be used. String produces redundant strings that take up memory space.
Method Chain
In fact, it's implemented with return this.
StringBuilder Expansion Principle (Important)
Looking at the source code and video, the notes are not clear. Simply speaking, it is to organize the larger length of the number, then copy the original data in, and then assign the reference variable value. The old array loses the reference value and will be recycled as garbage.
package com.test056; public class Test01 { /** * Testing variable character sequences, StringBuilder (thread insecurity, high efficiency), StringBuffer (thread security, low efficiency) * */ public static void main(String[] args){ StringBuilder sb = new StringBuilder();//Character array length is initially 16 StringBuilder sb1 = new StringBuilder(32);//Character array length initially 32 StringBuilder sb2 = new StringBuilder("ABCD");//The initial length of the character array is 4 + 16 = 20, followed by append, which is four characters, value []= {a','b','c','d', / u0000, / u0000...}. sb2.append("efg"); sb2.append(true).append(321).append("aaa");//The return this sentence in the source code can build a method chain //Distinguish the following two ways of writing String two = new String("a"); for(int i = 0;i<1000;i++){ two = two +i; } System.out.println(two); //String creates a total of 1002 objects StringBuilder one = new StringBuilder("a"); for(int i = 0;i<1000;i++){ one.append(i); } System.out.println(one);//There are only two objects to write this way. } }
Episode 58: Additions to common methods of using array StringBuilder and StringBuffer
StringBuilder is the same as StringBuffer square attribute method, except that StringBuffer method is preceded by synchronized method.
-
Common methods:
delete
append
reverse
replace
package com.test059; public class Test059 { public static void main(String[] args){ StringBuilder sb = new StringBuilder("abcdefgh"); sb.append(true).append("ABC").append('a');//Method Chain System.out.println(sb); sb.delete(3, 5);//The deletion method is [start end] header without tail System.out.println(sb); sb.replace(3, 5, "Hello"); System.out.println(sb);//The replacement method is to replace the character of [start end] with the specified character System.out.println(sb.length()); } }