Question about creating a String object in Java

Source: Internet
Author: User

I recently saw a discussion about how to create several objects in string S = new string ("XYZ") + new string ("XYZ") on the forum.

 

In Java, except for the eight basic types, all other types are class objects and references. Therefore, "XYZ" is a string object in Java. For a String object, its object value cannot be modified, that is, it is immutable.

 

But in the following program:

Public class teststring {<br/> Public static void main (string ARGs []) {<br/> string S = "hello"; <br/> S = "Java "; <br/> string S1 = "Java"; <br/> string S2 = new string ("Java"); <br/> system. out. println (s); <br/> system. out. println (S = S1); <br/> system. out. println (S = S2); <br/>}< br/>/* output: <br/> * Java <br/> * true <br/> * false <br/> */<br/>

The result of printing s is "Java". It seems that the string variable referenced by S has been modified. However, if you understand the mechanism of JVM (Java Virtual Machine) when processing string variables, you will know that this is not the case.

 

During JVM's work, a piece of memory space will be created and saved to the string object. We call this memory space a string pool.

For the statement string S = "hello"; When JVM sees "hello", it creates a String object to store it in the string pool and returns its reference to the string variable S.

Statement S = "Java"; When JVM sees "Java", a new String object is created in the string pool to store it, then return the reference of the New String object to the string variable S. The original string object "hello" is still in the string pool and does not disappear. It cannot be modified.

So we only changed the reference of variable S, but did not change the object referenced by it, because the value of the string object cannot be modified.

 

String S1 = "Java"; JVM first checks whether the string "Java" can be found in the string pool. If yes, it returns its reference to S1; otherwise, a New String object is created, put it in the string pool. Here, because S = "Java" exists, the object has been referenced, so both S and s1. So S = S1 returns true. (Note: The comparison operator = is used to compare whether two references reference the same object in memory for non-basic types ).

String S2 = new string ("Java"); first, the JVM should check whether the string "Java" can be found in the string pool. If it is found, nothing should be done; otherwise, create a new String object and put it in the string pool. Because of the New Keyword, the string object storage "Java" will be created in the memory (not in the string pool), and the memory (not in the string pool) string object is returned to S2. So S = S2 will return false because they reference not the same object.

 

Therefore, for the statement string S = new string ("XYZ") + new string ("XYZ ");

JVM first creates a String object storage "XYZ" in the string pool, and then creates a String object storage "XYZ" in the memory due to the new keyword ";

Then, because the "XYZ" object already exists in the string pool, the second new statement will not create an object in the string pool, but will only create a String object in the memory;

The last two strings are added. A String object "xyzxyz" is created in the string pool and referenced to S.

Therefore, a total of four string objects will be created.

 

<End>

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.