Java Review--string

Source: Internet
Author: User


1. Instance process of string

The maintenance of Java string is the embodiment of an element of enjoyment, and we know that there are two ways in which instances of string are:

String str = "Hello world"; String str2 = new string ("Hello World");

The way in which the former is literal is this: first look for "Hello World" in the string pool, if it does not exist, a "Hello World" is created in the string pool, and the reference to the object is returned to Str If it exists in the string pool, it is returned directly from the value in the string pool;

The concrete flow of the latter object is this: first look for "Hello World" in the string pool, if it does not exist, create one in the string pool, and then go to the heap to create a "Hello World" object, and return the reference in the heap to str2; If the object exists in the string pool, skip creating in the string pool and create a "Hello World" directly in the heap and return the reference.

Through the above you will understand:

Why

"Hello world" = = "Hello World" is true;

But

New string ("Hello world") = = new String ("Hello Wrold") is false;

While

"Hello world" = = new String ("Hello World") is also false;

In addition, the string Pool is a single-string-to-the-element factory, with the literal way of a single-element factory model. Everyone knows StringBuffer performance is better than string,stringbuilder performance is better than StringBuffer, but why still use String? The reason for this is that they say that the performance is when the string is modified, while the basic operation of the real character, and the string is obtained from the system, the string is the fastest and the least maintained object.


2. Equal judgment of string

    String overrides the Equals method, and the Equals method of object is to directly compare the addresses of two objects, so the use of comparisons on strings is not very significant, and to determine whether the two strings are the same, we should judge that The overridden Equals method is used. The specific code is as follows:

     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;         while  (n-- != 0)  {            if   (v1[i++] != v2[j++])              return false;        }         return true;        }    }     return false;    }
3, String other considerations

1), the string has no attribute exposure, the internal property is generally private and final type, such as offset, count, value;

2), the string rewrite the Equals method, then also must rewrite the Hashcode method;

3), the string cannot be inherited because he is a final type of class;

4), the string content cannot be changed because it is stored internally using a final type of char array, so it cannot be changed;

5), the advantage of string is his speed, because his design is a kind of enjoy meta-design mode.


Java Review--string

Related Article

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.