Some attention to Java's Sting.intern ()

Source: Internet
Author: User

Today, when reading a book encountered such a problem, for the String.intern () method has some knowledge and views. First we look at its API

The idea is that the Intern () method records the first occurrence of an instance reference in a constant pool, but not in jdk1.6. The string instance that will be first encountered in jdk1.6

A copy is placed in the persistence generation, and the return is a reference to the string in the permanent generation.

Such as

First, the running environment is jdk1.7, and will be true and false after running. Because StringBuilder creates instances on the Java heap, the Intern () method implementation will no longer replicate the instance.

This is true only if the first occurrence of the instance reference is recorded in a constant pool. S2 returns false because the string "Java" has already occurred before the StringBuilder () method is executed.

This means that the default in the string constant pool already has its reference, no longer conforms to the "first occurrence principle", and therefore returns false. and the "computer Software" string is the first occurrence, so

Returns true. The detail point is that the reference to the "Java" string has long been available. Here is a reference to the book "in-depth understanding of Java virtual Machines". I hope you can exchange more ideas, thank you for watching.

Some attention to Java's Sting.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.