String的源碼大家應該都能看懂,這裏就不一一分析咯,重點講一下equals()和hashcode()方法,然後看一下String類常用方法的實現,就當一起溫習一下咯。
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence {
/** 保存String的字節數組 */
private final char value[];
/** 緩存這個String的hash值 */
private int hash; // Default to 0
/** use serialVersionUID from JDK 1.0.2 for interoperability */
private static final long serialVersionUID = -6849794470754667710L;
/**
* 1、Object的 hashCode()返回該對象的內存地址編號,而equals()比較的是內存地址是否相等;
* 2、需要注意的是當equals()方法被重寫時,hashCode()也要被重寫;
* 3、按照一般hashCode()方法的實現來說,equals()相等的兩個對象,hashcode()必須保持相等;
* equals()不相等的兩個對象,hashcode()未必不相等
* 4、一個類如果要作爲 HashMap 的 key,必須重寫equals()和hashCode()方法
*/
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
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;
}
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}
/**
* 指定下標的char
*/
public char charAt(int index) {
if ((index < 0) || (index >= value.length)) {
throw new StringIndexOutOfBoundsException(index);
}
return value[index];
}
/**
* 是否以 prefix 爲前綴
*/
public boolean startsWith(String prefix) {
return startsWith(prefix, 0);
}
/**
* 是否以 suffix 爲後綴
*/
public boolean endsWith(String suffix) {
return startsWith(suffix, value.length - suffix.value.length);
}
/**
* 該String對象 是否滿足 regex正則表達式
*/
public boolean matches(String regex) {
return Pattern.matches(regex, this);
}
/**
* 字符替換
*/
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;
}
}
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++;
}
return new String(buf, true);
}
}
return this;
}
/**
* 子串替換
*/
public String replaceAll(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceAll(replacement);
}
/**
* 子串替換,只替換第一個
*/
public String replaceFirst(String regex, String replacement) {
return Pattern.compile(regex).matcher(this).replaceFirst(replacement);
}
/**
* 按 regex 切割成多個子串
*/
public String[] split(String regex) {
return split(regex, 0);
}
/**
* 剪切指定範圍的字符串
*/
public String substring(int beginIndex) {
if (beginIndex < 0) {
throw new StringIndexOutOfBoundsException(beginIndex);
}
int subLen = value.length - beginIndex;
if (subLen < 0) {
throw new StringIndexOutOfBoundsException(subLen);
}
return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
}
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);
}
/**
* 獲取該String 對應的 char[]
*/
public char[] toCharArray() {
// Cannot use Arrays.copyOf because of class initialization order issues
char result[] = new char[value.length];
System.arraycopy(value, 0, result, 0, value.length);
return result;
}
/**
* 大小寫轉換
*/
public String toLowerCase() {
return toLowerCase(Locale.getDefault());
}
public String toUpperCase() {
return toUpperCase(Locale.getDefault());
}
/**
* str在本String對象中第一次出現的下標
*/
public int indexOf(String str) {
return indexOf(str, 0);
}
/**
* str在本String對象中最後一次出現的下標
*/
public int lastIndexOf(String str) {
return lastIndexOf(str, value.length);
}
}