“This is the first day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021”

String constant pool and Integer constant pool

The String constant pool

String is final and cannot be overridden or inherited, creating a new object in heap memory each time it is used.

String s1 = "arrom";
String s2 = new String("arrom");
String s3 = "arrom";
String s4 = new String("arrom");

System.out.println(s1==s2);
System.out.println(s1==s3);
System.out.println(s1==s4);
System.out.println(s2==s4);

false
true
false
false
Copy the code

If compared using ==, s1 and S3 above are equal because they point to the same heap memory address. S2 and S4 above are not equal to the others because they point to different heap memory addresses. “Arrom” was added to the string constant pool when s1 was created, and “arrom” was added to the string constant pool when S3 was created. But when s2 and S4 are created, a new object is created. Instead, you can use the String object’s intern() method to fetch it from the cache pool.

String s1 = "arrom";
String s2 = new String("arrom").intern();
String s3 = "arrom";
String s4 = new String("arrom");

System.out.println(s1==s2);
System.out.println(s1==s3);
System.out.println(s1==s4);
System.out.println(s2==s4);

true
true
false
false
Copy the code

S1 and S2 have the same heap memory address

Integer’s constant pool

Values created with Integer between -128 and 127 will be placed in the constant pool, but new values will not be placed in the constant pool

Integer a1 = -129;
Integer a2 = -129;
System.out.println(a1 == a2);
Integer b1 = -128;
Integer b2 = -128;
System.out.println(b1 == b2);
Integer c1 = 127;
Integer c2 = 127;
System.out.println(c1 == c2);
Integer d1 = 128;
Integer d2 = -128;
System.out.println(d1 == d2);

false
true
true
false

Copy the code

Use the new

Integer e1 = 127; Integer e2 = new Integer(127); System.out.println(e1 == e2); // Result is false Integer f1 = 127; Integer f2 = new Integer(127); System.out.println(f1 == f2.intValue()); // Result is trueCopy the code

Explain your understanding of String and StringBuffer and StringBuilder

Inside the String is a final char array (immutable).

AbstractStringBuilder, AbstractBuffer, AbstractStringBuilder, AbstractStringBuilder, AbstractStringBuilder, AbstractStringBuilder, AbstractStringBuilder, AbstractStringBuilder, AbstractStringBuilder, AbstractStringBuilder, AbstractStringBuilder, AbstractStringBuilder, AbstractStringBuilder, AbstractStringBuilder, AbstractStringBuilder, AbstractStringBuilder, AbstractStringBuilder, AbstractStringBuilder, AbstractStringBuilder So it’s variable.

abstract class AbstractStringBuilder implements Appendable, CharSequence { char[] value; int count; AbstractStringBuilder() { } AbstractStringBuilder(int capacity) { value = new char[capacity]; } @Override public int length() { return count; } public int capacity() { return value.length; }}Copy the code

Both StringBuilder and StringBuffer objects are created by calling super(Capacity); Initialize the array.

public StringBuilder() {
    super(16);
}
Copy the code
public StringBuffer() {
    super(16);
}
Copy the code

When they call Append, they call the append method of the parent class.

public AbstractStringBuilder append(Object obj) {
    return append(String.valueOf(obj));
}

public AbstractStringBuilder append(String str) {
    if (str == null)
        return appendNull();
    int len = str.length();
    ensureCapacityInternal(count + len);
    str.getChars(0, len, value, count);
    count += len;
    return this;
}
Copy the code

Inside the appEnd method, it calculates the length of the string to insert, and then executes ensureCapacityInternal(count + len); Count + len refers to the length of the new string after insertion.

Private void ensureCapacityInternal(int minimumCapacity) {private void ensureCapacityInternal(int minimumCapacity) { If (minimumcapacity-value. length > 0) {value = array. copyOf(value, newCapacity(minimumCapacity)); Private int newCapacity(int minCapacity) {private int newCapacity(int minCapacity) {private int newCapacity = (value.length << 1) + 2; If (newCapacity -mincapacity < 0) {newCapacity = minCapacity; } // newCapacity is less than or equal to 0, or exceeds the maximum value of Integer -8. / / or you use the new expansion and calculated the length of the return (newCapacity < = 0 | | MAX_ARRAY_SIZE - newCapacity < 0)? hugeCapacity(minCapacity) : newCapacity; }Copy the code

Finally, getChars is called to create a new array and change the length of the current values array

public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
    if (srcBegin < 0) {
        throw new StringIndexOutOfBoundsException(srcBegin);
    }
    if (srcEnd > value.length) {
        throw new StringIndexOutOfBoundsException(srcEnd);
    }
    if (srcBegin > srcEnd) {
        throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
    }
    System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
}
Copy the code

Instead of creating new objects, we keep expanding the array. Efficiency is naturally a lot higher.

Many of the methods inside StringBuffer use the synchronized keyword, so it is thread-safe,

String, StringBuilder, and StringBuffer apply to scenarios

  1. String works with a small number of String operations
  2. StringBuilder is suitable for single-threaded situations where a large number of operations are performed in the character buffer
  3. StringBuffer A condition that uses multiple threads to perform a large number of operations in a character buffer

What is fail-fast

Fail-fast is an error detection mechanism for Java collections. Fail-fast is possible when iterating through a collection while modifying the collection or when multiple threads make structural changes to the collection. Remember, it’s a possibility, not a guarantee. In fact, the throw ConcurrentModificationException. The checkForComodification() method is invoked when the iterators of the set call the next() and remove() methods. The main purpose of this method is to test the expectedModCount == expectedModCount? If range throw ConcurrentModificationException, resulting in a fail – fast mechanism. ModCount is the value that changes each time the number of collections is changed.