String resident memo

Source: Internet
Author: User
Related blog address:
Http://www.cnblogs.com/artech/archive/2007/03/04/663728.aspxhttp://www.cnblogs.com/ASPNET2008/archive/2009/06/28/1512807.html below is a personal exercise: Code

// String detention exercise by mcjeremy
// When S1 is declared, this string does not exist in the detention pool, so it is placed in
// String. isintered (S1)
String S1 = "ABC123 ";
// When S2 is declared, no memory is allocated because the string already exists in the detention pool.
// S2 and S1 point to the same reference
String S2 = "ABC123 ";
// The actual call behind S3 is Concat, so it is dynamically created.
// Therefore, a new memory is allocated.
String S3 = S1 + S2;
// When S4 is created, the compiler will optimize it and call ldstr. Therefore, it will find that the detention pool has
// If ABC123 exists, no memory is allocated. At this time, S1, S2, and S4 all point to the same reference.
String S4 = "ABC" + "123 ";

String S5 = "123 ";
// When S6 is created, Concat is actually called. Therefore, it is created dynamically.
// Therefore, a new memory is allocated.
String S6 = "ABC" + S5;

Console. writeline (object. referenceequals (S1, S2); // true
Console. writeline (object. referenceequals (S1, S3); // false
Console. writeline (object. referenceequals (S1, S4); // true
Console. writeline (object. referenceequals (S1, S6); // false
Console. writeline (string. isinterned (S3 )?? "Null"); // null
Console. writeline (string. isinterned (S1 )?? "Null"); // ABC123
Console. writeline (string. isinterned (s6 )?? "Null"); // ABC123

 

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.