Java jdk1.7 after Sting intern () method differs from the previous

Source: Internet
Author: User

public static void Main (string[] args) {
String S1 = new StringBuilder ("Computer"). Append ("Software"). toString ();//In the heap
System.out.println (S1.intern () ==s1);//Intern Returns a value pointing to the heap "reference to computer software" with S1 pointing to the same piece of heap memory, same, returns True

String d= "class";//Add "class" string constant to the constant pool in the method area
String s2 = new StringBuilder ("CLA"). Append ("ss"). ToString ();//stringbuilder is created in the heap
System.out.println (S2.intern () ==s2);//If already in the constant pool, intern () after jdk1.7 returns a reference to a string in the heap, so it differs from the string constant class in the method area. Returns false

If both jdk1.6 and before, two return false. Because the previous intern () directly returns a reference to the corresponding string in the constant pool in the method area. The reference to the object in the heap must be different from the StringBuilder

}

Java jdk1.7 after Sting intern () method differs from the previous

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.