string constant pool and intern method

Source: Internet
Author: User
Tags constant

Read a few articles about the Intern method, summarized as follows:

First, the main points of knowledge:

1. String constants declared using quotation marks are generated directly in the string constant pool

2, Intern method in jdk1.6 and jdk1.7 a little difference, the main reason is that the constant pool in jdk1.7 moved to the heap

3, the use of intern method will be a great degree of memory savings


Two ways to create strings in Java:

There are two types of string object creation in Java,

A literal form, such as String str= "Hello",

The other is to use the standard method of constructing the object, such as String str=new string ("Hello");

These two are often used, and there are some differences in memory. It's all a JVM. To reduce the duplication of string objects, it maintains a special memory, which is the string constant pool


Working principle

When the literal form of a string object is created in the code, the JVM first checks the literal, and if a reference to a string object of the same content exists in the string constant pool, the reference is returned, otherwise the new string object is created and the reference is placed in a string constant pool. and returns the reference.

String str1= "Hello"

The JVM detects this literal, and here we think that there is no object with the "Hello" content. The JVM finds the existence of a string object with a string constant pool that does not have the content "Hello", then the string object is created, then a reference to the object just created is put into the string constant pool, and the reference is returned to the variable str1.


String str2= "Hello"

The JVM also detects this literal, and the JVM finds the existence of the "Droid" string object by finding the string constant pool, and returns the reference to the variable str2 for the existing string object. Note that new string objects are not recreated here.

Verify that str1 and str2 point to the same object, which we can System.out.println through this code (STR1 = = str2);

The result is true.


String str3 = new String ("Hello");

When we use new to construct a string object, a new string object is created, regardless of the reference to the object with the same content in the string constant pool. So let's test it with the code below,

String str3 = new String ("Hello");
System.out.println (str1 = = STR3);

The result, as we think, is false, which indicates that the two variables point to a different object.


Three, JDK1.6 and JDK1.7 in the Intern method difference:

1, in JDK1.6

In JDK1.6, the constant pool is placed in the perm area (which belongs to the method area), which is completely separate from the heap area.

in JDK1.6, the role of the Intern () method:

Checks if there is a string in the string pool that is "Hello", returns the string in the pool if it exists, and if it does not, the method adds "Hello" to the string pool before returning its reference.

(1)

	public static void Main (string[] args) {
		//Add string in constant pool Hello
		string s1= "Hello";
		Because "Hello" already exists in the constant pool, this string is returned as String
		S2=s1.intern ();
		SYSTEM.OUT.PRINTLN (S2);       Hello
		//s1 and S2 all point to the constant pool "Hello"
		System.out.println (S2==S1);   True
	}


(2)

	public static void Main (string[] args) {
		//create string in heap and add string in constant pool Hello  because string constants that are declared with quotation marks are generated
		directly in the string constant pool String S1=new string ("Hello");
		Because "Hello" already exists in the constant pool, this string is returned as String
		S2=s1.intern ();
		SYSTEM.OUT.PRINTLN (S2);       Hello
		//Because S1 points to a string in the heap, and S2 points to a string in a constant pool, so the address is different (= = is determined by the address)
		System.out.println (S2==S1);   False
	}

(3)

	public static void Main (string[] args) {
		//create string in heap "Hello World"  create "Hello" and "world" string separately in the constant pool
		s1=new String ("Hello") +new string ("World");
		Because "Hello World" does not exist in the constant pool, the string is added and the reference
		string S2=s1.intern () is returned;
		SYSTEM.OUT.PRINTLN (S2);       HelloWorld
		//Because S1 points to a string in the heap, and S2 points to a string in a constant pool, the address is different (= = The address is judged)
		System.out.println (S2==S1);   False
	}

Note: True is output in jdk1.7 and is described below.


2, in the JDK1.7

In JDK1.7, a constant pool is placed in the heap.

Normally, a reference to a string object created with new is not put into a string constant pool, but if you want to add a reference to the string constant pool using JDK1.7, you can use the Intern method. After calling intern, first check to see if there is a reference to the object in the string constant pool, and if so, return the reference to the variable, otherwise the reference is added and returned to the variable.


	public static void Main (string[] args) {
		//create string in heap "Hello World"  create "Hello" and "world" string separately in the constant pool
		s1=new String ("Hello") +new string ("World");
		/*
		 * because there is no "Hello World" in the constant pool, but there is this string in the heap,
		 * So the reference S1 is returned, that is, S2 also points to the string in the heap
		 *
		/String S2=s1.intern ();
		SYSTEM.OUT.PRINTLN (S2);       HelloWorld
		//So address same
		System.out.println (S2==S1);   True
		
		String s3= "HelloWorld";
		A reference to "HelloWorld" already exists in the constant pool, so S3 also points directly to the string
		System.out.println (S3==S1) in the heap;  True
	}


about why the Intern () method can save memory, see Java Technology-Do you really understand the intern () method of the String class?

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.