[Java Development Road] (8) The invariance of the Illustrated string

Source: Internet
Author: User

we use the following photos to illustrate the invariance of Java.
1. Declaring a string
String s = "abcd";
s stores a reference to a string object. The arrows in the picture below represent this kind of storage reference.
2. Assigning a string variable to another string variable
String s2 = s;
The S2 variable stores the same reference value. So, two variables point to the same string object.
3. Merging strings
s = s.concat("ef");
s now stores a reference to the newly generated string object.
4. Summary
Once a string is created on the memory (heap), the string does not change. We should note that all methods of the string class do not change the string itself, but instead return a new string.
If we need a string that can be changed, we can use StringBuffer or StringBuilder. Otherwise, because each time a new string is created.











[Java Development Road] (8) The invariance of the Illustrated string

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.