当前位置:网站首页>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"
边栏推荐
猜你喜欢
搭载开源鸿蒙系统的嵌入式XM-RK3568工业互联方案
C语言:打印菱形
错误:为 repo ‘oracle_linux_repo‘ 下载元数据失败 : Cannot download repomd.xml: Cannot download repodata/repomd.
Change Jupyter Notebook default open directory
Difference Constraint - Graph Theory
解决pycharm每次新建项目都要重新pip安装一些第三方库等问题
排序第三节——交换排序(冒泡排序+快速排序+快排的优化)(5个视频讲解)
数据库索引原理
【模板】树链剖分 P3384
基于布朗运动的文本生成方法-LANGUAGE MODELING VIA STOCHASTIC PROCESSES
随机推荐
P1505 [National Training Team] Tourism Tree Chain Breakdown
sklearn数据预处理
vlucas/phpdotenv phpdotenv获取变量内容偶尔出现返回false
unity第一课
Sklearn data preprocessing
Anaconda use proxy
接口测试概念
web自动化测试有哪些工具和框架?
Snake game, C language
入门cv必读的10篇baseline论文
HDU - 3183 A Magic Lamp Segment Tree
Record a failure to upgrade the client's APP database version number
定时任务组件Quartz
神经网络优化器
C语言:汽水瓶详解
软件测试的岗位会越来越少吗?
View log common commands
Win10桌面图标排列混乱
Lottie系列一:介绍与使用
排序第一节——插入排序(直接插入排序+希尔排序)(视频讲解26分钟)