So just to review the basics of Java on the web, see String and StringBuffer and here I’m just going to implement the class StringBuffer, MyStringBuffer (the name of our own StringBuffer class) does a performance test for concatenating strings.

Both String and StringBuffer are implemented internally by character arrays. However, the character array inside a String is fixed in length on the first assignment and cannot be changed, whereas the character array inside a StringBuffer is redundant in length. Increase the size of the array dynamically, making it look like a variable-length array.

When doing a lot of String concatenation, it is highly recommended to use StringBuffer rather than String because String concatenates strings and you need to create a String object to hold the String to be concatenated. A StringBuffer concatenated String simply extends the space of the String array and does not create a new StringBuffer object, which is why StringBuffer is faster than String.

Here’s a simple implementation of StringBuffer, called MyStringBuffer.

public class MyStringBuffer{ private int capacity = 16; Private int length = 0; Private char[] value; public MyStringBuffer() { value = new char[capacity]; } public MyStringBuffer(String str) { if(str == null) { return; } length = str.length(); // STR The length of the string is greater than the initial capacity. If (capacity < length) {capacity += length; value = new char[capacity]; System.arraycopy(str.toCharArray(), 0, value, 0, length); } else { value = new char[capacity]; System.arraycopy(str.toCharArray(), 0, value, 0, length); } } public void append(String str) { insert(length, str); } public void append(char c) { insert(length, String.valueOf(c)); } public void insert(int pos, String str) { if(pos < 0 || pos > length || str == null) { return; While ((length + str.length()) > capacity) {capacity += length + str.length(); char[] newValue = new char[capacity]; System.arraycopy(value, 0, newValue, 0, length); value = newValue; } char[] temp = str.toCharArray(); System. arrayCopy (value, pos, value, pos + temp.length, length-pos); System.arraycopy(temp, 0, value, pos, temp.length); length += temp.length; } public void insert(int pos, char c) { insert(pos, String.valueOf(c)); } public void delete(int start) { delete(start, length - 1); } public void delete(int start, int end) { if(start < 0 || start > length || end < 0 || end > (length - 1) || start > length) { return; } System.arraycopy(value, end, value, start, length - end); length -= (end - start); } public void reverse() {*char[] temp = new char[value.length]; for(int i = 0, j = length; i < length; i++, j--) { temp[i] = value[j - 1]; } value = temp; For (int I = 0; i < length / 2; i++) { char temp = value[i]; value[i] = value[length - i - 1]; value[length - i - 1] = temp; } } public int length() { return length; } public String toString() { char[] realValue = new char[length]; System.arraycopy(value, 0, realValue, 0, length); return new String(realValue); } public int capacity() { return capacity; }}Copy the code

Here is MyStringBuffer’s test class, MyStringBufferTest.

package com.wenshixin.character; import org.junit.Test; public class MyStringBufferTest { final int TIMES = 1000000; Final double DIVISOR = 1000000.0; Public void constructorTest() {MyStringBuffer MyStringBuffer = new MyStringBuffer("Weizhiwen"); System.out.println(myStringBuffer); MyStringBuffer longStringBuffer = new MyStringBuffer("It's a long road, the only key."); System.out.println(longStringBuffer); } @Test public void lengthAndCapacityTest() { MyStringBuffer myStringBuffer = new MyStringBuffer("Weizhiwen"); System.out.println("length:"+myStringBuffer.length()); System.out.println("capacity:"+myStringBuffer.capacity()); MyStringBuffer longStringBuffer = new MyStringBuffer("It's a long road, the only key."); System.out.println("length:" + longStringBuffer.length()); System.out.println("capacity:" + longStringBuffer.capacity()); } @Test public void reverseTest() { MyStringBuffer myStringBuffer = new MyStringBuffer("Weizhiwen"); myStringBuffer.reverse(); System.out.println(myStringBuffer); } @Test public void insertTest() { MyStringBuffer myStringBuffer = new MyStringBuffer("Weiwen"); myStringBuffer.insert(3, "zhi"); System.out.println(myStringBuffer); myStringBuffer.insert(0, "I'm "); System.out.println(myStringBuffer); myStringBuffer.insert(myStringBuffer.length(), "!" ); System.out.println(myStringBuffer); myStringBuffer.insert(myStringBuffer.length(), "\nIt's a long road, the only key."); System.out.println(myStringBuffer); myStringBuffer.insert(-1, "nothing"); System.out.println(" insert value below lower limit, display: "+ myStringBuffer); myStringBuffer.insert(myStringBuffer.length() + 2, "nothing"); System.out.println(" insert value above upper limit, display: "+ myStringBuffer); } @Test public void appendTest() { MyStringBuffer myStringBuffer = new MyStringBuffer(); myStringBuffer.append("I'm Weizhiwen"); System.out.println(myStringBuffer); myStringBuffer.append("!" ); System.out.println(myStringBuffer); } @Test public void deleteTest() { MyStringBuffer myStringBuffer = new MyStringBuffer("I'm not Weizhiwen!" ); myStringBuffer.delete(3, 7); System.out.println(myStringBuffer); } @Test public void stringPerformenceTest() { String string = "1"; Long startTime = System.currentTimeMillis(); System.out.println(startTime); for(int i = 0; i < TIMES; i++) { string += "0"; } Long endTime = System.currentTimeMillis(); System.out.println(endTime); double avgTime = (endTime - startTime) / DIVISOR; System.out.println("String average stitching time: "+ avgTime +" milliseconds." ); } @Test public void stringBufferPerformenceTest() { StringBuffer stringBuffer = new StringBuffer("1"); Long startTime = System.currentTimeMillis(); System.out.println(startTime); for(int i = 0; i < TIMES; i++) { stringBuffer.append("0"); } Long endTime = System.currentTimeMillis(); System.out.println(endTime); double avgTime = (endTime - startTime) / DIVISOR; System.out.println("StringBuffer averages a splicing time: "+ avgTime +" milliseconds. ); } @Test public void myStringBufferPerformence() { MyStringBuffer myStringBuffer = new MyStringBuffer("1"); Long startTime = System.currentTimeMillis(); System.out.println(startTime); for(int i = 0; i < TIMES; i++) { myStringBuffer.append("0"); } Long endTime = System.currentTimeMillis(); System.out.println(endTime); double avgTime = (endTime - startTime) / DIVISOR; System.out.println("MyStringBuffer average stitching time is: "+ avgTime +" milliseconds. ); }}Copy the code

I did the loop 1000 times, 10,000 times, 100,000 times, and 1000,000 times respectively, and the result was the average splice time t(StringBuffer) < t(MyStringBuffer) < t(String).

By implementing StringBuffer myself, I have deepened my understanding of the StringBuffer and Stringer classes, but the Java foundation needs to be strengthened.

Personal GitHub address: github.com/weizhiwen, huan…