The essence of a string

Source: Internet
Author: User

The essence of a string

Observation 1 (the string instance is directly assigned a value to the string type variable ):

 

Analysis:

The debugging shows that the memory addresses of the two variables are the same. The memory address actually points to the String constant zone.

Illustration:

Principle:

To create a String object, the system first scans the constant area for strings with the same value. If yes, the system returns the address corresponding to the constant area.

 

 

 

 

Observation 2 (instantiate an object of the string type through the new Keyword ):

Analysis:

In this case, why do I use the new keyword to create a string instance? The memory address is not the same as the memory address in Case 1. The reason is the essence of the new keyword.

New:

1. Open up heap memory space or constant Zone

2. Create an object

3. Call Constructor

4. Return the opened memory address

From this analysis, the new memory area is opened up, and the memory address naturally returned will not be the same.

 

 

 

 

Observation 3:

Thoughts:

From the normal logic analysis, a and B point to the reference of the same object. Case B = "bb"; then, the value of variable a should change. The value of a does not change here because it varies with the character string.

Illustration:

 

Principle:

If you modify the content of a variable of the string type, you cannot modify the string stored in the variable, but scan the constant area according to the new string, if not, open a new space to store this string and return a new memory space address.

 

 

 

 

Summary of string nature:

1. Create a string. the system first scans the constant area for strings with the same value. If yes, the system returns the address corresponding to the constant area.

2. if you modify the content of a variable of the string type, you cannot modify the string stored in the variable, but scan the constant area according to the new string, if not, open a new space to store this string and return a new memory space address.

3. The character string is constant (the value cannot be changed). Once created in the constant area, the lifecycle is released as the application ends.

4. strings are stored in the constant area, and the scope is a public area.

 

 

 

Interview practice questions:

Isn't the string variable? String s = "abc"; s = "123" Doesn't it change?

A:

Here, the storage value of variable s is indeed changed from "abc" to "123 ". However, this only changes the different references pointed to by variable s. "Abc" is a string instance object,

When "abc" is created, the constant zone in the heap area is scanned first, and there is no "abc". If yes, the address is directly returned. If not, a new region is opened and a new address is returned.

Once created in the constant area, the lifecycle is released as the application ends.

Therefore, "abc" is not changed when "123" is created ".

 

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.