Intern method of Java string _java

Source: Internet
Author: User

The first thing we should be aware of is that the intern method of the string class in JDK1.6 and JDK1.7 is different:

The Intern in JDK1.6:

When you call the Intern method, you first go to the constant pool to see if there is the same value as the current string value, and if it does, it returns a reference to the string value in the constant pool, which, if it does not exist, copies the string from the original heap into the constant pool.

The Intern in JDK1.7:

When you call the Intern method, you first go to the constant pool to see if there is the same value as the current string value, and if it does, it returns a reference to the string value in the constant pool, and if it does not, only the reference to that string in the original heap is placed in the constant pool. Does not copy the entire string into the constant pool.

This also means that JDK1.6 and JDK1.7 are different for situations where the string does not exist in a constant pool.

The following examples are used to verify and interpret:

Instance:

public static void Main (string[] args) { 
  string str = "str" +new String ("01"); ① 
  Str.intern (); ② 
  String str1 = "s Tr01 "; ③ 
  System.out.println (str = = str1); 
   
  String str2 = new String ("Str01"); ④ 
  Str2.intern (); ⑤ 
  string str3 = "Str01"; ⑥ 
  System.out.println (str2 = = Str3 ); 
   
  String STR4 = "Str01"; ⑦ 
  String str5 = new String ("str") +new string ("01"); ⑧ str5.intern 
  (); ⑨ 
  

The output results under JDK1.6 are:

False
False
False

Explain:

① creates a String object, str, with a value of "Str01" in heap memory, and creates a "str" and "01" constants in the constant pool;

② execution will first go to the constant pool to see if there is a constant value of "Str01", the discovery does not exist, JDK1.6 practice is that the string "Str01" in the constant pool also generate a copy;

③ execution creates a "Str01" object in the constant pool, discovers that it already exists, and therefore does not create a new one;
The first output is false because Str points to the "Str01" of the heap memory, while the str1 points to the "Str01" in the constant pool;

④ execution creates a string object str2 with a value of "Str01" in heap memory while creating a constant in the constant pool with a value of "Str01";

⑤ execution will first go to the constant pool to see if there is a constant value of "Str01", if found to exist, return the constant reference directly;

⑥ executes a constant that has a value of "Str01" in the constant pool and is not created if it is found to exist;

The second output is false because the str2 points to the "Str01" of the heap memory and STR3 to the "Str01" in the constant pool;

⑦ creates a constant with a value of "Str01" in the constant pool when executing;

⑧ execution creates a string object STR5 with a value of "Str01" in heap memory while creating a "str" and "01" constants in the constant pool;

⑨ execution will go to the constant pool to see if there is a constant with a value of "Str01", and find that there is a direct return to the constant reference;
The third output is false because the STR5 points to the "Str01" of the heap memory, while STR4 points to "Str01" in the constant pool;

The output results under JDK1.7 are:

True
False
False

Explain:

found that only the first output is different, so we only explain the first reason:

① creates a String object, str, with a value of "Str01" in heap memory, and creates a "str" and "01" constants in the constant pool; (This is no different from the JDK1.6)

② will first go to the constant pool to see if there is a constant with a value of "Str01" and find that it does not exist, JDK1.7 is to copy the reference of "Str01" in the heap memory to the constant pool;

③ execution creates a "Str01" object in the constant pool, discovers that it already exists, and therefore does not create a new one;
then both STR and STR1 will point to the value of "Str01" in the heap memory, so the two are equal;

The above is the JDK1.6 and JDK1.7 in the string class of the Intern method comparison, there is a difference, the need for friends can refer to.

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.