当前位置:网站首页>The String class objects created by the JVM memory allocation and the difference between equals and = =
The String class objects created by the JVM memory allocation and the difference between equals and = =
2022-08-09 07:33:00 【hash crazy】
Definition:
String s1 = "uml";String s2 = "uml";String s3 = new String("uml");String s4 = new String("uml");System.out.println(s1==s2);System.out.println(s3==s4);System.out.println(s1.equalsspan>(s4));
Judging code output results
A. true false true
B. false true false
Parse
This question examines the memory allocation of objects created by the String class in the JVM and the difference between equals
and ==
.
==
Compare address and valueequals
compares values
s1==s2
compares the address and value. From the above figure, we know that the two references point to the same address, so returntrue
.s3 == s4
compares the addresses of two space objects created by new, so the values are the same, but the addresses are different, returnfalse
.s1.equals(s3)
compares the content and returnstrue
.
- Objects created with
new
are in theheap
- The
temporary variable
in the function is in thestack area
string
in java is in thestring constant area
This question is extremely imprecise, and the answer depends on the specific JVM type and specific JVM version.The following analysis is based on the widely used Oracle HotSpot virtual machine:
First, String str1 = "abc", when compiled to bytecode, is actually equivalent to: String str1 = "abc".intern()For JDK 1.6, the intern() method is like this: if "abc" is encountered for the first time, the string "abc" itself is copied into the string constant pool, and if "abc" is encountered again later, which directly returns a reference to the constant pool.Note that the constant pool of JDK 1.6 exists in the permanent generation of the JVM, and for the implementation of the Hotspot virtual machine, the permanent generation actually corresponds to the method area in the java virtual machine specification!
As of JDK 1.7, the intern() method does not make a copy of the string instance, but only records the first occurrence of the instance reference in the string constant pool.Of course, the most important thing is that the string constant pool in JDK 1.7 is on the heap!
For details, please refer to the discussion on pages 42 and 57 of the book "In-depth understanding of the JAVA virtual machine"
边栏推荐
猜你喜欢
随机推荐
postgresql窗口功能
解决pycharm每次新建项目都要重新pip安装一些第三方库等问题
高项 04 项目整体管理
分布式事务的应用场景
半导体新能源智能装备整机软件系统方案设计
【MySQL】update mysql.user set authentication_string=password(“123456“) where User=‘root‘; 报错
74HC595 chip pin description
Invoker 2019CCPC Qinhuangdao Station I Question Simple DP
力扣208,实现Trie(前缀树)
【Template】Tree Chain Segmentation P3384
C语言:调整奇数偶数顺序
DSP+ARM+FPGA高速PCIE/千兆网口信号仿真介绍
Colors that Tkinter can choose from
vlucas/phpdotenv phpdotenv获取变量内容偶尔出现返回false
Record a failure to upgrade the client's APP database version number
入门cv必读的10篇baseline论文
链表专项练习(三)
Learning Notes---Machine Learning
DIMP:Learning Discriminative Model Prediction for Tracking 学习判别模型预测的跟踪
VOC format label to YOLO format