String class source code Analysis

Source: Internet
Author: User

    1. The Equals () method, which is defined in the object class, so that every class in Java has that method, and for the Equals () method of the object class, it is the same as the reference that invokes the Equals () method. That is, whether these two references point to the same object.

    2. public boolean equals (Object obj) {return (this = = obj);}

3. The Equals () method of String overrides the Equals () method of Object

4. For the Equals () method of the String class, it is to determine whether the current string is consistent with the contents of the passed in string.

Public boolean equals (object anobject) {    if (this == anObject) {         return true;    }     if (anobject instanceof string) {        string  anotherstring =  (String) anobject;        int n =  count;        if (N==anotherstring.count) {             char v1[] = value;             char v2[] = anotherString.value;             int i = offset;             int j = anotherString.offset;       &nbSp;     while (n--! =0) {                 if (v1[i++]!=v2[j++]) {                     return false;                 }                 return true;             }                     }         return false;    }}

5. For the Equality judgment of a string object, use the Equals () method instead of = =.

6.String is a constant, and its object cannot be changed once it has been created. When you use the + sign to stitch a string, a new string object is automatically generated instead of appending content to the original string object.

7.String Pool (string pooling):

8.String s = "AAA";

1) Look for the "AAA" object in the string pool, if it does not exist, create an "AAA" object in the string pool, and then return the address of the "AAA" object in the string pool to the reference variable s, This causes S to point to the "AAA" string object in the string pool.

2) If present, do not create any objects, directly return the "AAA" object address in the string pool and assign the S reference.

9.String s = new String ("AAA");

1) First find the object "AAA" in the string pool. If so, it does not create an "AAA" object in the string pool, creates an "AAA" string object directly in the heap (heap), and then returns the address of the "AAA" object in the heap to the S reference, causing S to point to the "AAA" string object created in the heap.

2) if not, first create an "AAA" object in the string pool, then create an "AAA" object in the heap, and then return the address of the "AAA" object in the heap to the S reference, causing S to point to the "AAA" object created in the heap.





























This article is from the "Java" blog, so be sure to keep this source http://5737386.blog.51cto.com/5727386/1662193

String class source code Analysis

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.