About String.intern ()

Source: Internet
Author: User

String.intern () will actually return the instance that generated the String content for the first time.

The so-called content is "abc", "123" The contents of this string

1.

String S1 = "a"; ([email protected])

So S1 is the first instance of generating content "a" @100, so if

String S1 = "a"; ([email protected])

String s2 = "a"; ([email protected])

Because their content is the same, there is no explicit new, so the same instance is used, and the natural underlying char[] is the same instance

2.

String S1 = "a"; ([email protected]) (char[]@200)

String s2 = new String ("a"); ([email protected]) (char[]@210)

However, it is important to note that, although two String instances are different, their underlying char[] is the same instance

3.

String S1 = "AB"; ([email protected]) (char[]@200)

String s2 = "a";

String s3 = s2 + "B"; ([email protected]) (char[]@210)

Use variables to stitch strings, end string instances differently, char[] instances are also different

4.

String S1 = "AB"; ([email protected]) (char[]@200)

String s2 = "a";

String s3 = s2 + "B"; ([email protected]) (char[]@210)

String i1 = S1.intern (); ([email protected])

String i3 = S3.intern (); ([email protected])

Finally find I1 = = i3, they are the same instance, this is the role of intern, it will return the first instantiation of this string content instance, here is S1, even if s3! = S1, S3.intern () will also return S1, because S1 is the first

5.

string S1 = new String ("AB"); ([email protected])

String s2 = S1.intern (); ([email protected])

S1! = S2 Note that because S1 uses the new string (), it actually generates a string instance when "AB" appears, so string s1 = new String ("AB"); This one-line program generates 2 String instances, although their internal char[] is the same. Eventually, S1.intern () returns the first instance you don't see

About String.intern ()

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.