Java Program Ape interview written book What is the mechanism of string creation and storage

Source: Internet
Author: User

In the Java language. The string plays a very critical data. There are two cases where the declaration and initialization of a string are as follows:
(1) for a string S1=new string ("ABC") statement with a string s2=new string ("ABC") statement, there are two reference objects S1, S2, and two contents of the same string object "ABC". Their addresses in memory are different. New objects will always be generated just by using new.

(2) for string S1 = "ABC" statement with string s2 = "ABC" statement. There is a string pool in the JVM that holds very many string objects and can be shared, S1, S2 refers to objects in the same constant pool. Because the implementation of string uses the flyweight design pattern. When a string constant is created. string s = "abc", for example, will first look in the string constant pool to see if the same string is already defined, and its inference is based on the return value of the String class equals (Object obj) method.

Assuming that it is already defined, the reference to it is obtained directly, and there is no need to create a new object, assuming it is undefined, create the object first, then add it to the string pool and return its reference.

Because string is immutable, it cannot be changed once it is created, so a string object can be shared without causing confusion in the program.
In detail:
String s= "ABC"; Put "ABC" in the constant area, which is generated at compile time.
String s= "AB" + "C"; Convert "AB" + "C" to the string constant "ABC" into the constant area.
String S=new string ("abc"); Put "ABC" into the heap at the time of execution.
Like what:
String s1= "ABC"; An "ABC" string object is stored in the constant area.
String s2= "ABC"; S2 references an object in a constant area, so a new object is not created
String S3=new string ("abc")//Creates a new object in the heap
String S4=new string ("abc")//Creates a new object in the heap
For ease of understanding. The ability to artificially decompose the execution of string s = new String ("ABC") statements into two procedures: the first procedure is the process of creating a new object, that is, the new string ("abc"), and the second procedure is the process of assigning a value, that is, the string s=.

Because the second procedure simply defines a variable of type string named S, assigning a reference to the object of type string to S, the new object is not created in this procedure.

In the first procedure, the new string ("ABC") calls the constructor of the string class:
public string (string original) {
Body
}
When this constructor is called, a string constant is passed in. So the statement new string ("ABC") is also equivalent to the "ABC" and the new string () two operations. Assume that "ABC" does not exist in the string pool. A string constant "ABC" is created and added to the string pool, assuming it exists. is not created. Then new String () creates a new object in the heap. So Str3 and STR4 point to a different string object in the heap, and the address naturally is not the same.

5 5 to see.

Two kinds of string storage methods

Extension: For a variable s of type string. is the assignment statement s=null and s= "" the same?
For an assignment statement s=null, where S is a reference to a string type, it does not point to whatever string it is. The assignment statement s= "" is a reference to a string type that points to another string (the value of the string is "", or an empty string).
Common written Questions:
New String ("ABC") has created several objects?
Answer: one or two. Assuming that there is an "ABC" in the constant pool, then just create an object, assuming that there is no string "abc" in the constant pool, then two objects will be created.


From

p=180 ">java Program Ape interview written book official website

Java Program Ape interview written book What is the mechanism of string creation and storage

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.