A string is actually a char collection
The string is just roughly looked at, basically all operations on a char array, including whether to judge if it is empty, or to specify subscript data, and so on.
The string class is final decorated, so it cannot be inherited, implementing interfaces such as Java.io.Serializable, Comparable<string>, and Charsequence, stating that a string can be serialized and deserialized , and supports custom string comparisons.
The main record equals and Hashcode two methods, both of which are overridden by the object class.
The Equals method of the object class actually uses the "= ="
public boolean equals (Object obj) {
return (this = = obj);
}
And Hashcode is not written in Java, do not say more
The following is a string override
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;
}
The first is to determine whether the object address is the same, the address is the same directly return true, the address is not the same to determine whether the string length is the same, and finally the string of each character by contrast.
public int hashcode () {
int h = hash;
if (h = = 0 && value.length > 0) {
Char val[] = value;
for (int i = 0; i < value.length; i++) {
H = * H + val[i];
}
hash = h;
}
return h;
}
The hash address algorithm is mainly h = * H + val[i], each time is the last calculation of the hash value multiplied by 31 and then add the current character encoding value, because there is an int is bound to have a limit, when the string exceeds the upper limit, the correctness of the hashcode can not be guaranteed, So it can be inferred that there are hashcode characters with the same hashcode.
The string is the same, the hash must be the same, the hash is the same, and the string is not.