Explore Java Strings

Source: Internet
Author: User

Links: http://mccxj.github.io/blog/20130615_java-string-constant-pool.html

String Impressions

Beginner Java, there is already a string is immutable, the conclusion of the coffin, the interpretation is usually: it is final.

However, string is a literal statement, which is a feature that other types do not have (except native types). In addition, Java also has a string constant pool that is used to store string literals, not on the heap, but in the method area inside the existence.

A preliminary study of literal and constant pools

The inside of a string object is stored in a character array, so look at the following example:

String m = "Hello,world";

String n = "Hello,world";

string u = new String (m);

String v = new String ("Hello,world");

What happens to these statements?

    1. A 11-length char array is assigned, and a string consisting of this char array is assigned to the constant pool, which is then referenced by M to refer to the string.

    2. Use N to refer to the string inside the constant pool, so the same object is referenced by N.

    3. A new string is generated, but the inner character array refers to an array of characters inside M.

    4. A new string is also generated, but the inner character array refers to the character array inside the string within the constant pool , meaning that it is the same character array as U.

If we use a graph to represent it, this is probably the case (using a dashed line just means that there is nothing special about the two):

Explore Java 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.