当前位置:网站首页>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"
边栏推荐
- HDU - 3183 A Magic Lamp 线段树
- 高项 03 项目立项管理
- 错误:为 repo ‘oracle_linux_repo‘ 下载元数据失败 : Cannot download repomd.xml: Cannot download repodata/repomd.
- 用tensorflow.keras模块化搭建神经网络模型
- XILINX K7 FPGA+RK3399 PCIE驱动调试
- DIMP:Learning Discriminative Model Prediction for Tracking 学习判别模型预测的跟踪
- Classes and Structures
- tianqf's problem-solving ideas
- SSM integration development case
- 力扣208,实现Trie(前缀树)
猜你喜欢
工信部等四部门推动绿色智能家居产品下乡
【报错】Root Cause com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
Unity first lesson
PyTorch中 torch.nn与torch.nn.functional的区别
虚拟机网卡报错:Bringing up interface eth0: Error: No suitable device found: no device found for connection
链表专项练习(四)
排序第二节——选择排序(选择排序+堆排序)(两个视频讲解)
【机器学习】降维代码练习
c语言位段
SSM整合开发案例
随机推荐
Invoker 2019CCPC秦皇岛站I题 简单DP
【Oracle 11g】Redhat 6.5 安装 Oracle11g
分布式理论
错误:为 repo ‘oracle_linux_repo‘ 下载元数据失败 : Cannot download repomd.xml: Cannot download repodata/repomd.
Anaconda 使用代理
Lottie系列二:高级属性
学习小笔记---机器学习
子路由及路由出口配置
es6 基础知识详解 变量 字符串 解构赋值 函数 对象 从入门到精通
Important news丨.NET Core 3.1 will end support on December 13 this year
Difference Constraint - Graph Theory
3.MySQL插入数据, 读取数据、Where子句和Order By关键字
DIMP:Learning Discriminative Model Prediction for Tracking 学习判别模型预测的跟踪
web自动化测试有哪些工具和框架?
P1505 [国家集训队]旅游 树链剖分
信息反馈平台的设计与实现(一、项目设计)
Learning Notes---Machine Learning
MUV LUV EXTRA 2019CCPC Qinhuangdao Station J Question KMP
软件测试的岗位会越来越少吗?
设备指纹详解之识别垃圾账号