Analysis of the string in the dark horse programmer-Java

Source: Internet
Author: User

-------------------- Android training, Java training, and hope to communicate with you! ---------------------

In Java, the string class is a string class. Its objects are often different from other objects, which is hard to understand. After several days of study, I would like to summarize some of the features of the string class.

1. equivalent to a constant:

The string is a constant and cannot be modified after it is created (you can use the stringbuffer class if you want to modify it at any time ).

2. storage location:

Generally, this method is used to define an object.

String STR = new string ("test ");

However, the string class can also be assigned a value directly.

String STR = "test ";

What is the difference between the two methods? This requires figuring out how strings are stored in Java. This issue involves two important storage areas in Java: Heep and string pool ). Heap is used to store objects, including the special attributes of the object (that is, non-static attributes, methods are stored in the Method Area, which is not discussed here ). The string pool is used to store the currently used non-repeated strings for ease of use, so as not to repeatedly define the same string.

The process of the statement string STR = new string ("test"); is like this. First, check whether there is a "test" string in the string pool. If not, add the "test" string to the pool, create an object in the heap (there is also a "test" string) and return the object address to the STR variable.

The execution process of the statement string STR = "test"; is like this. First, check whether there is a 'test 'string in the string pool. If not, add a "test" string to the pool, then return the address of the "test" string in the pool to the STR variable. As you can see, no object is created here.

In this way, there will be some important features

(1) string str1 = "test ";

String str2 = "test ";

The str1 and str2 values are equal. Because their values are all the "test" addresses in the pool (each string has only one copy in the pool)

(2) string str1 = new string ("test ");

String str2 = new string ("test ");

  The str1 and str2 values are not equal. Because their values are the addresses of two independent objects, but the strings saved in the object are the same (after the first statement is executed, there is a "test" string in the pool ).

3. Equals method and "="

Two methods are available to determine whether two objects (A and B) are the same object.

(1) system. Out. println (A. Equals (B ));

(2) system. Out. println (A = B );

What is the difference between equals and "=?

"=" Is used to directly compare whether the values of two variables (the object address) are the same. If they are the same object, true is returned; otherwise, false is returned.

  Equals is a method used to determine whether an object is the same. It is defined in the object class as "=". If the address is the same, true is returned. Otherwise, false is returned. However, the string type overide method returns true if the two string objects have the same string. If the two string objects have the same string, false is returned if the two values are different. That is to say, it compares whether the values of the corresponding strings are the same.

Therefore:

String A = new string ("ABC ");

String B = new string ("ABC ");

String c = "ABC ";

String d = "ABC ";

A. Equals (B) returns true.

The result of a = B is false.

C. True of the equals (d) Result

The result of c = D is true.

A. Equals (c) returns true.

The above three points are my summary. If you have any mistakes, please correct them. Thank you.

----------------------- Android training, Java training, and hope to communicate with you! ----------------------

See http://edu.csdn.net/heima for details

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.