[Path to Java Development] (8) illustration of immutability of strings

Source: Internet
Author: User

[Path to Java Development] (8) illustration of immutability of strings
The following figure shows the immutability of Java.
1. Declare a string

String s = "abcd ";
S Stores references to string objects. The arrows in the image below indicate the storage reference.
2. assign a string variable to another string variable.
String s2 = s;
The s2 variable stores the same reference value. Therefore, two variables point to the same string object.
3. Merge strings
S = s. concat ("ef ");
S now stores references to newly generated string objects.
4. Summary
Once a string is created on the memory (HEAP), the string will not change. We should note that all methods of the String class do not change the String itself, but return a new String.
If we need a variable string, we can use StringBuffer or StringBuilder. Otherwise, a new string is created each time.

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.