当前位置:网站首页>Detailed explanation of string constant pool and intern() of string
Detailed explanation of string constant pool and intern() of string
2022-07-24 05:25:00 【Xinxin vegetation】
String constant pool
One 、 What is a constant pool ?
Constant pool stay java It is used to save the... Determined at compile time , Compiled class A piece of data in the file , Includes about classes , Method , Constants in interfaces, etc , It also includes string constants .
String constant pool It's a fixed size Hashtable, Used to store strings .
In the pile (heap) A part of memory space is allocated as constant pool area .
Two 、 Why should there be a string constant pool ?
String Class is used very frequently .
for fear of JVM First, create a large number of string objects , Then garbage collection .
To save memory and improve efficiency .
3、 ... and 、 How to generate objects in the string constant pool ?
1. The main generation methods
- Use it directly Double quotes It was announced String object , direct Stored in constant pool in .
- Use it directly new keyword The created string object will First, look in the string constant pool , If not found, create one in the string constant pool , Then create a string object in the heap ; If you find it , Just create a string object directly in the heap .
- If Not in double quotes Declarative String object , Not directly new Out object , have access to intern Method realization Add constant pool .( Such as String s1 = new String(“hello”) + new String(“world”); Created s1 object )
2. String Splicing generation
1 String splicing
public class StringTest {
public static void main(String[] args) {
String a = "wechat" + "zhou";
}
}
wechat and zhou All strings , At compile time , The compiler will automatically optimize this line of code For the following code
public class StringTest {
public static void main(String[] args) {
String a = "wechatzhou";
}
}
2 Reference splicing
( In fact, it is the third point of the above main generation methods , Need to use intern() Method )
Reference splicing ( Such as s3) The final content of , Will not exist in the constant pool
String s1=“1”; Created 1 individual "1" object , In the constant pool .
String s2=new String(“1”); I created two "1" object , One in constant pool , In a heap District .
String s3=“1”+“1”; Created a "11" object , Not in constant pool , stay heap District .
public class StringTest {
public static void main(String[] args) {
String a = "we";
String b = a + "chat";
// The upper and lower meanings are the same
String c = "moz";
String d = "bad";
String f = c + d;
}
}
Because the referenced value cannot be determined at compile time
When there is a string reference (a+"chat" or c+d) When splicing ,Java The compiler will create a StringBuilder object , adopt append() Method to realize splicing .
"wechat" It will not be added to the string constant pool , however Can pass b.intern() Method is added to the constant pool
3 final Splicing
The effect is consistent with string splicing
public class StringTest {
public static void main(String[] args) {
final String a = "we";
final String b = "chat";
String c = a + b + "handsome";
}
}
use final The modified string is known at compile time Of , The above code will be optimized to
public class StringTest {
public static void main(String[] args) {
final String a = "we";
final String b = "chat";
String c= "wechathandsome";
}
}
Intern() Method
One 、intern() The role of methods
You can add to the constant pool
1 If in the constant pool There is The current string , will Go straight back to The current string .
2 If in the constant pool non-existent The current string , This string Put into constant pool , Back again .
Two 、intern() Return value of method
1 If in the constant pool There is The current string , will Go straight back to Reference to the current string .
2 If in the constant pool non-existent The current string :
if heap The object of this string exists in the area , Will The object is moved to the constant pool , And return the reference of the object .
if heap The object does not exist in the zone , It's in Constant pool creates this string , And return a reference to the string .
(String s="1" Equivalent to String s=“1”.intern())
Do the title
The constant pool is in the heap , But for ease of description , The following heap areas refer to the places after the heap area is removed from the constant pool area .
One 、String s = new String(“abc”) Several objects are created ?
Created 1 or 2 Objects .( Depends on whether the string constant pool originally exists abc character string )
1 If the string constant pool already exists abc character string , Then create an object , In the pile area .
2 If the string constant pool does not exist abc character string , Then create two objects , One in constant pool , One in the pile area .
Two 、 The output of the following two pieces of code
jdk6 and jdk The difference between districts : The constant area moves from outside the heap area to inside the heap area .
code snippet 1
public static void main(String[] args) {
String s = new String("1");
s.intern();
String s2 = "1";
System.out.println(s == s2);
String s3 = new String("1") + new String("1");
s3.intern();
String s4 = "11";
System.out.println(s3 == s4);
}
jdk6 Next false false
jdk7 Next false true
about s3 and s4
String s3 = new String(“1”) + new String(“1”);
Generated 2 End object , Namely In the string constant pool “1” and JAVA Heap Medium s3 The object to which the reference refers . In the middle 2 Anonymous new String(“1”) No discussion .
here s3 The reference object is ”11”, There is no “11”.
s3.intern(); take s3 Medium “11” String into String Constant pool in .
jdk6: stay perm Generate a... In the string constant pool of the area “11” The object of .
jdk7: Constant pool is not Perm Area and in heap District , There is no need to create another object , It is You can store references in the heap directly . This quote points to s3 Referenced object . That is to say, the reference address is the same .
Last String s4 = “11”; In this code ”11” It's a statement , It will be directly created in the constant pool , It is found that this string already exists , That is to point s3 A reference to an object . therefore s4 The quotation points to and s3 The same . So the final comparison s3 == s4 yes true.
about s and s2
String s = new String(“1”); Generated 2 Objects . In the constant pool “1” and JAVA Heap String object in .
s.intern(); Go to the constant pool and find “1” It's already in the constant pool , Do not change .
String s2 = “1”; Generate a s2 References to point to... In the constant pool “1” object .
and s Point to JAVA Heap String object in , Does not point to constant pool , Therefore, the two are not equal .
code snippet 2
public static void main(String[] args) {
String s = new String("1");
String s2 = "1";
s.intern();
System.out.println(s == s2);
String s3 = new String("1") + new String("1");
String s4 = "11";
s3.intern();
System.out.println(s3 == s4);
}
jdk6 Next false false
jdk7 Next false false
about s3 and s4
String s3 = new String(“1”) + new String(“1”); Generated 2 End object , Namely In the string constant pool “1” and JAVA Heap Medium s3 The object to which the reference refers .
String s4 = “11”; There is no constant pool “11” object , Create... In the string constant pool “11” String .s4 Point there .
s3.intern();, In constant pool “11” The object already exists , So there is no change .s3 Still original heap An object of the zone ( Not in constant pool ).s3 And s4 Different .
about s and s2
String s = new String(“1”); Has been generated in the constant pool “1” Object .
s.intern(); No change .
s2 Declarations are all address references directly from the constant pool .
s Point to heap District ( Outside the constant pool ) The object of “1”.
s and s2 The reference addresses of are not equal .
边栏推荐
- Introduction to reflection
- 【Pytorch】conv2d torchvision.transforms
- 1. Input a 100 point score from the keyboard and output its grade according to the following principles: score ≥ 90, Grade A; 80 ≤ score < 90, grade B; 70 ≤ score < 80, grade C; 60 ≤ score < 70, grade
- MySQL深入了解
- Career planning route
- 使用d2l包和相关环境配置的一些血泪心得
- C语言进阶篇 三.字符串函数和内存操作函数
- 空杯心态,重新开始
- thread
- Blue Bridge Cup 31 day sprint 21 day (C language)
猜你喜欢

How to avoid the most common mistakes when building a knowledge base?
![Knowledge record of College Physics C in advance in summer [update]](/img/c4/76b669c3229a365a5e2567f7fb824e.png)
Knowledge record of College Physics C in advance in summer [update]
![Embedded system transplantation [3] - uboot burning and use](/img/36/69daec5f1fe41bd3d0433d60816bba.png)
Embedded system transplantation [3] - uboot burning and use

JDBC encapsulates a parent class to reduce code duplication

明星逆市入局的NFT,如何能走出独立行情?

使用d2l包和相关环境配置的一些血泪心得

SSH service

C语言进阶篇 三.字符串函数和内存操作函数

Blue Bridge Cup 31 day sprint 21 day (C language)

Chapter5 foundation of deep learning
随机推荐
Learning some contents of vector and iterator
泛型和注解
scikit-learn笔记
关于numpy基础用法的一个整理
手写orm框架
Blue Bridge Cup 31 day sprint 21 day (C language)
ZY: modify host name
1. There is a fractional sequence: 2/1, 3/2, 5/3, 8/5, 13/8,... Program to sum the first 20 items of this sequence.
Relational database 10 minutes to understand MySQL
MySQL connection
统计学之样本和总体的关系: 样本成功比例+中心极限定理(样本均值)
C语言入门篇 一.分支和循环语句
Scikit learn -- steps of machine learning application development
【STL】Map &unordered_map
Installation and login login
C语言从入门到入土——操作符超详细总结
Basic knowledge of MySQL database
【Pytorch】conv2d torchvision.transforms
C语言入门篇 五.初识指针 六.初识结构体
Data annotation learning summary