Home >Java >javaTutorial >Example interpretation of String source code
String class internally maintains a value of type char[] to store strings. Relatively speaking, the source code is relatively simple.
The immutability of String is mainly reflected in three aspects:
The String class is defined as a final type. Cannot be inherited
The value[] in String is defined as final
All operations in String that generate new Strings are called at the bottom Array.copy or System.copy to generate a new String object
public String(String original) { this.value = original.value; this.hash = original.hash; } String(char[] value, boolean share) { // assert share : "unshared not supported"; this.value = value; }
The above two are a special type of constructor. The first one uses a ready-made String to initialize a new String object. The constructor method directly points the value of the new String object to the old value object. Since String is immutable, there is no need to recopy the value object here. The second constructor seems to destroy the immutability of the String type (String will also change when the parameter value changes), but this constructor is not declared as public and is only allowed to be used within the package. String() is declared as public char value[]) The bottom layer calls Array.copy to realize the underlying data copy. The above two constructors are no longer recommended to be used.
public String(char value[], int offset, int count) { if (offset < 0) { throw new StringIndexOutOfBoundsException(offset); } if (count < 0) { throw new StringIndexOutOfBoundsException(count); } // Note: offset or count might be near -1>>>1. if (offset > value.length - count) { throw new StringIndexOutOfBoundsException(offset + count); } this.value = Arrays.copyOfRange(value, offset, offset+count); }
The above constructor is more typical, and many other constructors are similar to it. Similar or the underlying constructor is called, the input parameters are a char array (byte[]), offset offset position and count offset. The bottom layer calls the Arrays.copy function to perform deep copying.
public String(StringBuffer buffer) { synchronized(buffer) { //保证线程安全 this.value = Arrays.copyOf(buffer.getValue(), buffer.length()); } } public String(StringBuilder builder) { this.value = Arrays.copyOf(builder.getValue(), builder.length()); }
The input parameters of the above two constructors are StringBuffer and StringBuilder respectively. The bottom layer calls Arrays.copyOf. The only difference is that StringBuffer is thread-safe and the synchronized keyword needs to be used in all calls.
static int indexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex) { if (fromIndex >= sourceCount) { return (targetCount == 0 ? sourceCount : -1); } if (fromIndex < 0) { fromIndex = 0; } if (targetCount == 0) { return fromIndex; } char first = target[targetOffset]; int max = sourceOffset + (sourceCount - targetCount); for (int i = sourceOffset + fromIndex; i <= max; i++) { /* Look for first character. */ if (source[i] != first) { while (++i <= max && source[i] != first); } /* Found first character, now look at the rest of v2 */ if (i <= max) { int j = i + 1; int end = j + targetCount - 1; for (int k = targetOffset + 1; j < end && source[j] == target[k]; j++, k++); if (j == end) { /* Found whole string. */ return i - sourceOffset; } } } return -1; } static int lastIndexOf(char[] source, int sourceOffset, int sourceCount, char[] target, int targetOffset, int targetCount, int fromIndex) { /* * Check arguments; return immediately where possible. For * consistency, don't check for null str. */ int rightIndex = sourceCount - targetCount; if (fromIndex < 0) { return -1; } if (fromIndex > rightIndex) { fromIndex = rightIndex; } /* Empty string always matches. */ if (targetCount == 0) { return fromIndex; } int strLastIndex = targetOffset + targetCount - 1; char strLastChar = target[strLastIndex]; int min = sourceOffset + targetCount - 1; int i = min + fromIndex; startSearchForLastChar: while (true) { while (i >= min && source[i] != strLastChar) { i--; } if (i < min) { return -1; } int j = i - 1; int start = j - (targetCount - 1); int k = strLastIndex - 1; while (j > start) { if (source[j--] != target[k--]) { i--; continue startSearchForLastChar; } } return start - sourceOffset + 1; } }
indexOf and lastIndexOf are mainly the underlying calls of the index and lastIndex functions. If you read through the code, you will find that the underlying implementation does not have a particularly awesome kmp algorithm. It is still one character per character. Character scanning is implemented. Among them, lastIndexOf still uses continue startSearchForLastChar; which is relatively rare.
public String replace(char oldChar, char newChar) { if (oldChar != newChar) { int len = value.length; int i = -1; char[] val = value; /* avoid getfield opcode */ while (++i < len) { if (val[i] == oldChar) { break; } } //如果找不到则返回this if (i < len) { char buf[] = new char[len]; for (int j = 0; j < i; j++) { buf[j] = val[j]; } while (i < len) { char c = val[i]; //替换 buf[i] = (c == oldChar) ? newChar : c; i++; } //返回新的String,利用上述包内的非public构造函数 return new String(buf, true); } } return this; }
replace is used to replace one character in the String object with another character. If the specified character cannot be found, it will return itself. If found, a new String object will be created and returned.
---End of recovery content---
The above is the detailed content of Example interpretation of String source code. For more information, please follow other related articles on the PHP Chinese website!