Java-the creation and storage mechanism of strings

Source: Internet
Author: User

In the Java language, strings play a very important role in the Declaration and initialization of strings by the following two scenarios:

1 "for string S1 = new String (" ABC ") statement with string s2 = new String (" ABC ") statement, there are two reference objects S1,s2, two contents of the same string object" ABC ", their addresses in memory are different. New objects will always be created if you use new.

2 for string S1 = "abc" and string s2 = "ABC" statements, there is a string pool in the JVM with many string objects and can be shared, s1,s2 referencing objects in the same common pool. Where string is in flyweight design mode, when a string constant is created, such as String s= "abc", the string constant pool is first searched for the existence of the same string definition, based on the return value of the Equals method in the String object. If defined, the definition is referenced directly, and no new object is created at this time, and if there is no definition, the object is created first, then added to the string pool and returned by his reference. Since strings are immutable classes, they cannot be modified once they are created, so string objects can be shared without causing confusion in the program.

In specific terms:

String s = "abc"; Put ABC in the constant zone, which is generated at compile time.

String s = "AB" + "C"; Convert "AB" + "C" to string constant "ABC" in constant area

string s =new string ("ANC"); Put ABC in the heap at run time

For example:

String S1 = "abc"; A string object that holds an "ABC" in the Constant area

String s2 = "abc";//S2 references an object in a constant area, so no new object is created.

String s3 = new String ("abc");//Creates a fresh object in the heap

String S4 = new String ("abc");//Create a new object in the heap

For ease of understanding, you can do it. String s = new String ("ABC") statement performs a classification of two procedures; The first procedure is the process of creating a new object, which is the new string ("abc"), and the second procedure is the assignment process, the String s=. Because the second procedure simply creates a variable of a string type, assigns a reference to the string object to S, so that no new object is created in this procedure. The first procedure, new string ("abc"), calls the construction method in the String class:

public string (string origin) {

//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. If ABC is not present in the string pool, a new string constant "ABC" is created and added to the string pool, and if it is not created, then new string () creates a new object in the heap, so S3 and S4 are pointing to different string objects, and the address is naturally different.



Extension: For a string type of variable s, the assignment statement s=null and s= "" is the same?

For an assignment statement s=null, where S is a reference to a string type, it does not point to either string. 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), so the two are not the same.

Common written Questions:

New String ("ABC") has created several objects?

Answer: one or two. If there is a string "abc" in the constant pool, then only one object is created, and if there is no string "abc" in the constant pool, then two objects are created (these two objects are in the string constant pool and heap respectively).

Finally, the groove: Why answer, the case will be approved by the two words do not pass it? I don't understand.







Java-the creation and storage mechanism of strings

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.