当前位置:网站首页>Storage principle of string
Storage principle of string
2022-06-29 20:50:00 【zhengmayusi】
Yes String String to make a brief introduction
- String Represents string type , Belong to Reference data type , Not a basic data type
- stay Java Use casually in In double quotation marks The are String object , for example :“abc” “hello”
- Java Specified in the , A string in double quotation marks , yes immutable Of , in other words "abc" From birth to death , immutable , Can't be "abcd", Nor can it become "ab"
- stay JDK The string enclosed in double quotation marks , for example :“abc” "def" All are Directly stored in the string constant pool of the method area Of
- Why are strings stored in the string constant pool ? Strings are used too often in actual development , in order to Improve the efficiency of execution
example1:
String s1 = "abcdef";
String s2 = "abcdef"+"xy";
These two lines of code create 3 String objects , All in String constant pool in
String s3 = new String("xyz");
Call the constructor to create a string , Use new A string object created in a way , In this code xyz Is from the string constant pool ; Everything enclosed in double quotation marks has a copy in the string constant pool .new Object is to open up space in heap memory
Code corresponding memory map :
note: Local variables are all created in stack memory , Local variables in code s3 What is kept is String The address of the object in heap memory
example2:
class User:
public class User {
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
}
class UserTest:
public class UserTest {
public static void main(String[] args) {
User user = new User(110," Zhang San ");
}
}
Strings enclosed in double quotation marks open up memory space in the string constant pool in the method area , so " Zhang San " In the string constant pool ; the new Of User Object opens up space in heap memory , in other words id = 110 Space opened up in heap memory , and name What is saved is its... In the string constant pool Memory address ;user As a local variable, the address is opened in the stack memory , What is saved is the memory address of the corresponding object in the heap memory ; The corresponding memory diagram is as follows :
Add :
Interview questions : The following code creates several objects ?
String s1 = new String("hello");
String s2 = new String("hello");
answer : altogether 3 Objects ! String constant pool 1 Objects (“hello”), In heap memory 2 Objects (2 individual String object )!( Memory diagram as above )
example3:
note: Garbage collector GC Don't release ( Recycling ) Constant
- In the following code ,s2 Of ”hello“ Whether it will be created ? What is the result of the output ?
String s1 = "hello";
String s2 = "hello";
System.out.println(s1==s2);
Can't ! Double quotes "hello" Is stored in the string pool of the method area ,s1 It has been created , so s2 No more creation , Instead, it refers directly to its address in the string constant pool . The output is zero true, because **== The double equal sign compares memory addresses **, Variables s1 and s2 It's all preserved "hello" Memory address in string constant pool , Therefore, the output true!
2. In the following code , What is the result of the output ?
String x = new String("xyz");
String y = new String("xyz");
System.out.println(x==y);
false! local variable x,y Separately saved String The address of the object in heap memory , and x、y The saved String The object is different , Are created in the heap memory respectively ! Therefore, the output false!
The memory diagram corresponding to the above code :
Through this case, we can also know , Comparison of string objects cannot be done with "“, and You should use String Class equals Method .Objec The bottom layer of the class is also ”",String Class overridden equals!
边栏推荐
- Win7 Easy Connect prompt: route selection connection failed. The current connection network may be abnormal. Please try again later
- 0/1分数规划专题
- Hangfire details
- Jupyter服务安装及启动
- What are the mainstream brands of smart door locks? What characteristics should we pay attention to when purchasing door locks?
- 2021 CCPC Harbin J. local minimum (thinking question)
- Explain PBR texture maps
- 计算成像前沿进展
- 2021 CCPC 哈尔滨 J. Local Minimum (思维题)
- Golang basic learning
猜你喜欢

0/1 score planning topic

STL教程6-deque、stack、queue、list容器
【云原生实战】KubeSphere实战——多租户系统实战

Explain PBR texture maps

Three.js开发:粗线的画法
![[notes] take notes again -- learn by doing Verilog HDL – 008](/img/7f/0ca73446247455ac4d8f9667083a87.png)
[notes] take notes again -- learn by doing Verilog HDL – 008

《强化学习周刊》第51期:PAC、ILQL、RRL&无模型强化学习集成于微电网络格控制:综述与启示

Bigder:自动化测试工程师

In depth good article | yolov5+deepsort multi-target tracking in-depth interpretation and testing (including source code)

mapbox-gl开发教程(十二):加载面图层数据
随机推荐
"Xiaodeng" active directory batch user creation in operation and maintenance
A Japanese Cherry sold at a sky high price of 1980 yuan. Netizen: I feel cheated after eating it
VoIP Push 在海外音视频业务中的应用
Mapbox GL development tutorial (12): loading surface layer data
"Xiaodeng" in operation and maintenance monitors user login operations in real time
「运维有小邓」审核并分析文件和文件夹访问权限
Tag based augmented reality using OpenCV
Comparable comparator writing & ClassCastException class conversion exception
Three.js开发:粗线的画法
"Operation and maintenance department has Xiao Deng" to review and analyze file and folder access rights
解释PBR纹理贴图(texture-maps)
广东二级造价工程师《造价管理》真题解析
Sentinel's quick start takes you through flow control in three minutes
Go deep into the underlying principles of go and rewrite redis middleware without secret
2021 CCPC 哈尔滨 E. Power and Modulo (思维题)
Proxmox cluster node crash handling
CorelDRAW2022全新版V24.1.0.360更新
[compilation principle] type check
导航【微机原理】
量子机器学习的基础和应用:一个简明文献综述