Detailed Explanation of StringBuffer Class---Java
- 1. The difference between String, StringBuffer and StringBuilder
- 2. Source code analysis
- 3. StringBuffer method
-
- 3.1 StringBuffer append (xxx): Provides many append() methods for string concatenation
- 3.2 StringBuffer delete (int start, int end): delete the content at the specified location
- 3.3 StringBuffer replace (int start, int end, String str): replace [start, end] position with str
- 3.4 StringBuffer insert (int offset, xxx): Insert xxx at the specified position
- 3.5 StringBuffer reverse (): reverse the current character sequence
- 3.6 StringBuffer also contains String methods
- 4. Efficiency comparison of String, StringBuffer and StringBuilder
1. The difference between String, StringBuffer and StringBuilder
String: an immutable character sequence; the bottom layer uses char[] to store
StringBuffer: a variable character sequence; thread-safe, low efficiency; the bottom layer uses char[] to store
StringBuilder: a variable character sequence; thread-unsafe, high efficiency ;The bottom layer is stored in char[]
[Note]: StringBuffer and StringBuilder are essentially the same, but StringBuffer is thread-safe, while StringBuilder is thread-unsafe; the basic operations of both are the same
2. Source code analysis
// String和StringBuffer的操作对应底层源码操作
String str = new String(); // char[] value = new char[0];
String str1 = new String("abc"); // char[] value = new char[]{'a','b','c'};
StringBuffer sb1 = new StringBuffer(); // char[] value = new char[16];
StringBuffer sb2 = new StringBuffer("abc"); // char[] value = new char["abc".length()+16];
System.out.println(sb1.length()); // 0
sb1.append("a"); // value[0] = 'a';
sb2.append('b'); // value[sb2.length()] = 'b';
3. StringBuffer method
3.1 StringBuffer append (xxx): Provides many append() methods for string concatenation
StringBuffer sb1 = new StringBuffer("abc");
sb1.append('a');
sb1.append(3);
sb1.append("def");
System.out.println(sb1);
You can add data in any way, see the constructor for details:
3.2 StringBuffer delete (int start, int end): delete the content at the specified location
StringBuffer sb1 = new StringBuffer("abca3def");
sb1.delete(2, 4);
System.out.println(sb1);
Output:
ab3def
3.3 StringBuffer replace (int start, int end, String str): replace [start, end] position with str
StringBuffer sb1 = new StringBuffer("ab3def");
sb1.replace(2, 4, "123");
System.out.println(sb1);
ab123ef
3.4 StringBuffer insert (int offset, xxx): Insert xxx at the specified position
StringBuffer sb1 = new StringBuffer("ab123ef");
sb1.insert(0, 2.3);
System.out.println(sb1);
2.3ab123ef
3.5 StringBuffer reverse (): reverse the current character sequence
StringBuffer sb1 = new StringBuffer("2.3ab123ef");
sb1.reverse();
System.out.println(sb1);
fe321ba3.2
3.6 StringBuffer also contains String methods
- public int indexOf (String str)
- public String substring (int start,int end)
- public int length()
- public char charAt(int n)
- public void setCharAt (int n ,char ch)
For details, see Java's String class method explanation
4. Efficiency comparison of String, StringBuffer and StringBuilder
public class StringBufferTest {
/**
* String StringBuffer StringBuilder效率对比
*
*/
@Test
public void test2() {
long startTime = 0L;
long endTime = 0L;
String str = "";
StringBuffer buffer = new StringBuffer("");
StringBuilder builder = new StringBuilder("");
startTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
str += i;
}
endTime = System.currentTimeMillis();
System.out.println("String执行时间:" + (endTime - startTime));
startTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
buffer.append(i);
}
endTime = System.currentTimeMillis();
System.out.println("StringBuffer执行时间:" + (endTime - startTime));
startTime = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
builder.append(i);
}
endTime = System.currentTimeMillis();
System.out.println("StringBuilder执行时间:" + (endTime - startTime));
}
}
String execution time: 3603
StringBuffer execution time: 6
StringBuilder execution time: 3