Many of the online discussions about intern method were incorrect or not rigorous, so I summarized the knowledge point by referring to a large number of official materials, hoping to make it clear. This interview focuses on the JVM’s memory model, depth of understanding of the String class, and knowledge of String constant pools.

Introduction to the string constant pool

In the JAVA language, there are eight basic types and a special type, String, which is a reference type. They are grouped together because they all provide a constant pool concept for faster and more memory efficient execution. A constant pool is like a cache provided at the JAVA system level. The eight basic types of constant pools are system-coordinated, with the String constant pool (called String constant pool for the String class) being a special case. The string constant pool in JDk6 was placed in the heap space from jdk7 in the permanent generation. . There are two main ways to use it (or how to ensure that the variable points to data in the string constant pool) : 1. Strings declared directly in double quotes are stored directly in the constant pool. String s = “abc”; 2. If a String is not declared with double quotes, you can use the String provided intern method, which is explained below. The string constant pool holds two things: 1. String objects, such as “ABC” above 2. A reference to a heap object.

Ii. Intern method introduction

Pre-knowledge:

1. When a String is concatenated, as long as one of them is a variable, the concatenated object is in the heap, equivalent to a new String(“XXX”) in the heap space (not in the String constant pool). Variable concatenation works by StringBuilder calling the Append method and then the toString method.

2. How many objects does new String(“ab”) create?

Two objects, one in the heap space and one in the string constant pool (bytecode instruction LDC). If you are interested, you can look at the compiled bytecode file.

3. New String(“a”)+new String(“b”)?

Six objects. If you are interested, you can look at the compiled bytecode file. By looking at the bytecode file:

Reference from official documentation: In JDK8, it does this if the String constant pool already contains a String equal to the String object (compared to equals), Returns a reference to the String in the String constant pool, otherwise add (or copy) the reference address of the current String (the reference address of the object in the heap) to the String constant pool and return, to save heap space, after all, is in the heap.

For example: String s = new String(“s”).intern(); New String(“s”) creates two objects, one in the heap space and one in the String constant pool. New String(“s”).intern() returns a reference to the object in the String constant pool to the s variable.

Analysis of interview questions

The interview questions are as follows. Determine whether the output is true or false. Here directly attached to the analysis, there are exercises behind.

String s = new String("1"); s.intern(); String s2 = "1" in the String constant pool before calling this method; System.out.println(s == s2); //jdk6:false jdk7/8:false String s3 = new String("1") + new String("1"); //s3 variable record address: new String("11"), heap. // After the last line of code, there is no "11" in the string constant pool!! s3.intern(); // Generate "11" in the string constant pool. In JDK6, a new object "11" is created in the permanent generation and thus has a new address. //jdk7/jdk8, instead of creating "11", add a new String("11") address to the heap space String s4 = "11"; Println (s3 == s4); system.out.println (s3 == s4); system.out.println (s3 == s4); //jdk6:false jdk7/8:trueCopy the code

Schematic diagram:

Iv.String intern method summary

Five. Consolidation exercises

String s = new String("a") + new String("b"); //new String("ab") String s2 = s.intern(); //jdk7/8 creates a reference to new String("ab") system.out.println (s2=="ab"); //jdk7/8 creates a reference to new String("ab") system.out.println (s2=="ab"); //jdk6:true jdk8:true System.out.println(s=="ab"); //jdk6:false jdk8:trueCopy the code
String s1 = new String("ab"); // Generates "ab" in the string constant pool s1.intern(); String s2 = "ab"; System.out.println(s1 == s2); //jdk8:falseCopy the code

Resources: B station is still silicon valley JVM tutorial Meituan technology: tech.meituan.com/2014/03/06/…