Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

As for Java source code, I believe that you have more or less familiar with the util package, such as ArrayList, HashMap, LinkedHashMap, as well as the previous we learned about multithreaded related classes, such as: Executors, CountDownLatch, CyclicBarrier, or Object, Integer, String, Thread, etc. in lang packages.

These are fairly common classes, but we don’t always know how they work. Some people write for four or five years, and they don’t even see the most common String implementation, and they complain about moving bricks every day. To be honest, learning source code is not just for the interview, more importantly, we read the process of source code, learn its design ideas, this idea can be put into practice in our project.

To sum up, reading source code has the following benefits:

  • Learn excellent design ideas;
  • Increase the interview strength, a sweep;
  • Flex your muscles during routine code reviews.

Second, String source code analysis

String is used quite a lot in everyday development, so today we’ll take a look at why it’s so popular.

2.1 Immutability

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
...
}
Copy the code

As you can see from the code, the String class is decorated with the final keyword, which means that the String class cannot be inherited and cannot be modified after creation.

The String class implements three interfaces simultaneously:

  • Serializable: Implements serialization, marks the interface for serialization, and cannot be serialized without implementing the interface.
  • Comparable: Compares the size of two instantiated objects
  • CharSequence: String is essentially a char array. This interface is a read-only character sequence.

2.2 Member Variables

/** The value is used for character storage. */ private final char value[]; /** Cache the hash code for the string */ private int hash; /** Use serialVersionUID from JDK 1.0.2 for interoperability */ private static final long serialVersionUID = -6849794470754667710L; /** * Class String is special cased within the Serialization Stream Protocol. */ private static final ObjectStreamField[] serialPersistentFields = new ObjectStreamField[0];Copy the code

String contains a char array value. Value is also final, which means that the array cannot be modified. In addition, the access permission of value is private, so the variable cannot be accessed externally. String also provides no manipulation of value, so value cannot be modified once generated.

2.3 Common Methods

How are some of the most common methods in the String class implemented

  • equals
Public Boolean equals(Object anObject) {if (this == anObject) {return true; If (anObject instanceof String) {String anotherString = (String)anObject; if (anObject instanceof String) {String anotherString = (String)anObject; int n = value.length; / / whether the length is equal to the if (n = = anotherString. Value. Length) {char v1 [] = value; char v2[] = anotherString.value; int i = 0; While (n--! = 0) { if (v1[i] ! = v2[i]) return false; i++; } return true; } } return false; }Copy the code
  • Interception string: substring
public String substring(int beginIndex, int endIndex) {
    if (beginIndex < 0) {
        throw new StringIndexOutOfBoundsException(beginIndex);
    }
    if (endIndex > value.length) {
        throw new StringIndexOutOfBoundsException(endIndex);
    }
    int subLen = endIndex - beginIndex;
    if (subLen < 0) {
        throw new StringIndexOutOfBoundsException(subLen);
    }
    return ((beginIndex == 0) && (endIndex == value.length)) ? this
            : new String(value, beginIndex, subLen);
}
Copy the code

As you can see from the source code, the method calls the new String() constructor for a qualified start/end index, and the constructor returns a new String with System.arrayCopy, which means that the method does not modify the original String.

Similarly, we can look at the source code of methods like replace and concat. They will not modify the original String, but will return a new String object, which is also immutable.