The underlying implementation of String

public final class String
    implements java.io.Serializable.Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];
Copy the code
You can see that String is modified by final, which means String cannot be inherited and you can also see that the bottom layer of String is actually an array of char, which is also modified by final to become a constant. Classes modified by fianL cannot be inherited. Methods modified by fianL cannot be overridden. Variables modified by fianL become constants and cannot be changed once they are initializedCopy the code

A String of assignment

New String(" XXX "); new String(" XXX "); new String(" XXX ")Copy the code
String str = "abc";
Copy the code
So instead of creating a new object on the heap, the "=" is going to search the constant pool and reference the address of that character if it's in the constant pool and reference the address character if it's not there, it's going to create that character in the constant pool and reference the address character if there are no two identical characters in the constant pool, that is to sayCopy the code
String str1 = "abc";
String str2 = "abc";
System.out.println(str1==str2);//true
// Both reference the same address, pointing to a character
Copy the code
String str2=new String("abc");
Copy the code
New String() means that a new object has been created, it means that a chunk of memory has been allocated on the heap, but it doesn't mean that the character is stored on the heap, it means that it stores an address, and that address still points to the character constant pool. Some people say that the character is initialized and cannot be changed, so how can I change itCopy the code
String str="abc";
     System.out.println(str);
     System.out.println(str+"456");
     System.out.println(System.identityHashCode(str));
     System.out.println(System.identityHashCode(str+"456")); The command output is ABC abc4561528902577
1927950199
Copy the code
You can see that the "+" sign seems to add new characters, but it is not the original object, the memory address is different and can only add characters, to modify the content of the character, you need to look at the following two classesCopy the code

StringBuffer and StringBuilder

So here’s a graph THAT I found, and you can see that Both StringBuffer and StringBuilder are descended from AbstractStringBuilder

To change characters, StringBuffer and StringBuilder both provide methods for adding characters: Append method for deleting characters: Delete method for reversing characters: revers method for replacing characters: The replace method has some other methods that I'll explore next timeCopy the code

Since both classes can modify characters, what’s the difference between the two?

StringBuffer is thread-safe, StringBuilder is thread-safe and in fact, StringBuffer locks some of its methods with the synchronized keyword so that it can support concurrent operations regardless of concurrency, StringBuilder is more efficient than StringBuffer because you don't need to lock and release locksCopy the code

Capacity expansion mechanism:

 public final class StringBuffer
    extends AbstractStringBuilder
    implements java.io.Serializable.CharSequence
{
    private transient char[] toStringCache;

    static final long serialVersionUID = 3388685877147921107L;

    public StringBuffer(a) {
        super(16);
    }

Copy the code
As you can see, the initial length of the StringBuffer array is 16 (as is the StringBuilder), and when it is too long, a scaling mechanism is triggered to create a new array that is twice as long as the original array +2 and copy the elements of the original arrayCopy the code
private int newCapacity(int minCapacity) {
    // overflow-conscious code expands 2 times +2
    // The overflow will be negative
    int newCapacity = (value.length << 1) + 2;
    if (newCapacity - minCapacity < 0) {
        newCapacity = minCapacity;
    }
    // the value of MAX_ARRAY_SIZE is integer. max_value-8, and the expected capacity (newCapacity) is 0
    return (newCapacity <= 0 || MAX_ARRAY_SIZE - newCapacity < 0)? hugeCapacity(minCapacity) : newCapacity; }Copy the code