/*
final修飾,不能被繼承,不可修改
java.io.Serializable:序列化接口僅用于標(biāo)識序列化。
序列化:對象轉(zhuǎn)換成字節(jié)流的一個(gè)過程,用來處理對象流的機(jī)制。
Comparable<String>:接口用于對兩個(gè)實(shí)例化對象比較大小。
CharSequence:只讀的字符序列;
抽象方法length(), charAt(int index), subSequence(int start, int end)
public String toString();
*/
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
hash = h = isLatin1() ? StringLatin1.hashCode(value)
: StringUTF16.hashCode(value);
}
return h;
}
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String aString = (String)anObject;
if (coder() == aString.coder()) {
return isLatin1() ? StringLatin1.equals(value, aString.value)
: StringUTF16.equals(value, aString.value);
}
}
return false;
}
}
final class StringLatin1 {
public static boolean equals(byte[] value, byte[] other) {
if (value.length == other.length) {
for (int i = 0; i < value.length; i ) {
if (value[i] != other[i]) {
return false;
}
}
return true;
}
return false;
}
public static int hashCode(byte[] value) {
int h = 0;
int length = value.length >> 1;
for (int i = 0; i < length; i ) {
h = 31 * h getChar(value, i);
}
return h;
}
}
final class StringUTF16 {
public static int hashCode(byte[] value) {
int h = 0;
int length = value.length >> 1;
for (int i = 0; i < length; i ) {
h = 31 * h getChar(value, i);
}
return h;
}
@HotSpotIntrinsicCandidate
public static boolean equals(byte[] value, byte[] other) {
if (value.length == other.length) {
int len = value.length >> 1;
for (int i = 0; i < len; i ) {
if (getChar(value, i) != getChar(other, i)) {
return false;
}
}
return true;
}
return false;
}
}
StringBuilder類源碼
特點(diǎn):
線程不安全,適合單線程中使用
具有數(shù)據(jù)緩存區(qū)
實(shí)現(xiàn)了java.io.Serializable接口,可序列化
public final class StringBuilder extends AbstractStringBuilder
implements java.io.Serializable, CharSequence{
static final long serialVersionUID = 4383685877147921099L;
@HotSpotIntrinsicCandidate
public StringBuilder() {
super(16);
}
@HotSpotIntrinsicCandidate
public StringBuilder(int capacity) {
super(capacity);
}
@HotSpotIntrinsicCandidate
public StringBuilder(String str) {
super(str.length() 16);
append(str);
}
public StringBuilder(CharSequence seq) {
this(seq.length() 16);
append(seq);
}
//個(gè)append 方法
public StringBuilder append(String str) {
super.append(str);
return this;
}
public StringBuilder append(StringBuffer sb) {
super.append(sb);
return this;
}
}
//抽線類,這個(gè)抽象類值得好好看看
abstract class AbstractStringBuilder implements Appendable, CharSequence {
byte[] value;//The value is used for character storage.
byte coder;//The id of the encoding used to encode the bytes in {@code value}.
int count;//The count is the number of characters used.
public AbstractStringBuilder append(String str) {
if (str == null) {
return appendNull();
}
int len = str.length();
ensureCapacityInternal(count len);
putStringAt(count, str);
count = len;
return this;
}
private AbstractStringBuilder appendNull() {
ensureCapacityInternal(count 4);
int count = this.count;
byte[] val = this.value;
if (isLatin1()) {
val[count ] = 'n';
val[count ] = 'u';
val[count ] = 'l';
val[count ] = 'l';
} else {
count = StringUTF16.putCharsAt(val, count, 'n', 'u', 'l', 'l');
}
this.count = count;
return this;
}
private void ensureCapacityInternal(int minimumCapacity) {
//移位運(yùn)算符;<<(左移)、>>(帶符號右移)、>>>(無符號右移)
int oldCapacity = value.length >> coder;
if (minimumCapacity - oldCapacity > 0) {
value = Arrays.copyOf(value,newCapacity(minimumCapacity) << coder);
}
}
private int newCapacity(int minCapacity) {
int oldCapacity = value.length >> coder;
int newCapacity = (oldCapacity << 1) 2;
if (newCapacity - minCapacity < 0) {
newCapacity = minCapacity;
}
int SAFE_BOUND = MAX_ARRAY_SIZE >> coder;
return (newCapacity <= 0 || SAFE_BOUND - newCapacity < 0)
? hugeCapacity(minCapacity): newCapacity;
}
private final void putStringAt(int index, String str) {
if (getCoder() != str.coder()) {
inflate();
}
str.getBytes(value, index, coder);
}
}