String source __jdk1.8 Source code

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.