当前位置:网站首页>String字符串的存储原理
String字符串的存储原理
2022-06-29 20:48:00 【zhengmayusi】
对String字符串做一个简单介绍
- String表示字符串类型,属于引用数据类型,不属于基本数据类型
- 在Java中随便使用双引号括起来的都是String对象,例如:“abc” “hello”
- Java中规定,双引号括起来的字符串,是不可变的,也就是说"abc"从出生到最终死亡,不可变,不能变成"abcd",也不能变成"ab"
- 在JDK当中双引号括起来的字符串,例如:“abc” "def"都是直接存储在方法区的字符串常量池中的
- 为什么字符串存储在字符串常量池中?字符串在实际开发中使用过于频繁,为了提高执行的效率
example1:
String s1 = "abcdef";
String s2 = "abcdef"+"xy";
这两行代码底层创建了3个字符串对象,都在字符串常量池中
String s3 = new String("xyz");
调用构造方法创建字符串,使用new的方式创建的字符串对象,这个代码中的xyz是来自字符串常量池中的;但凡是双引号括起来的都在字符串常量池中有一份。new对象的时候是在堆内存开辟空间
代码对应内存图:
note: 局部变量都是在栈内存开辟空间,代码中局部变量s3保存的是String对象在堆内存中的地址
example2:
类User:
public class User {
private int id;
private String name;
public User(int id, String name) {
this.id = id;
this.name = name;
}
}
类UserTest:
public class UserTest {
public static void main(String[] args) {
User user = new User(110,"张三");
}
}
双引号括起来的字符串都在方法区中的字符串常量池中开辟内存空间,故"张三"在字符串常量池中;所new的User对象在堆内存中开辟空间,也就是说id = 110在堆内存中开辟的空间,而name 保存的是其在字符串常量池中的内存地址;user作为局部变量则在栈内存中开辟地址,保存的是其在堆内存中对应对象的内存地址;对应的内存图如下:
补充:
面试题:以下代码创建了几个对象?
String s1 = new String("hello");
String s2 = new String("hello");
答:一共3个对象!字符串常量池中1个对象(“hello”),堆内存中2个对象(2个String对象)!(内存图如上)
example3:
note: 垃圾回收器GC不会释放(回收)常量
- 以下代码中,s2的”hello“是否还会被创建?输出的结果是什么?
String s1 = "hello";
String s2 = "hello";
System.out.println(s1==s2);
不会!双引号"hello"是存储在方法区的字符串常里池当中,s1已经创建过了,故s2不会再创建,而是直接引用其在字符串常量池当中的地址。输出的结果是true,因为**==双等号比较的是内存地址**,而变量s1和s2都是保存的"hello"在字符串常量池中的内存地址,故输出true!
2. 以下代码中,输出的结果是什么?
String x = new String("xyz");
String y = new String("xyz");
System.out.println(x==y);
false! 局部变量x,y分别保存了String对象在堆内存中的地址,而x、y保存的String对象是不同的,是各自在堆内存中所开辟的!故输出false!
上述代码所对应的内存图:
通过这个案例我们也可以知道,字符串对象的比较不能使用"“,而应该使用String类的equals方法。Objec类的底层也是”",String类重写了equals!
边栏推荐
- Nutch2.1分布式抓取
- 【译】十二因子应用(四)
- Win7 Easy Connect prompt: route selection connection failed. The current connection network may be abnormal. Please try again later
- 2021 CCPC 哈尔滨 E. Power and Modulo (思维题)
- Community interview -- jumpserver open source fortress in the eyes of an it newcomer
- Oracle保留字查询
- XSS vulnerability
- Chainsafe cross chain bridge deployment tutorial
- The foundation and application of quantum machine learning: a concise literature review
- The explain statement in MySQL queries whether SQL is indexed, and several types in extra collate and summarize
猜你喜欢
「运维有小邓」AD域委派
Navigation exercises [microcomputer principles] [exercises]
File contains vulnerability
. NETCORE unified authentication authorization learning - first authorization (2)
Mapbox GL development tutorial (12): loading surface layer data
"Xiaodeng" in operation and maintenance monitors user login operations in real time
导航【微机原理】
The reason why the log analysis tool of "operation and maintenance" is used more and more frequently
0/1 score planning topic
Fastadmin background setting radio button
随机推荐
At least 3 years for learning amplifier?
JMeter BeanShell explanation and thread calling
tmux设置
解释PBR纹理贴图(texture-maps)
广东二级造价工程师《造价管理》真题解析
「运维有小邓」实时监控用户登录操作
Deploy web using gunicorn Py application
智能门锁主流品牌有哪些?选购门锁时要注重产品的哪些特性?
Cmake开发-多目录工程
Final review [microcomputer principle]
Three. JS development: drawing of thick lines
【编译原理】语义分析
annotation
Hangfire details
[a must for reptiles - > scrapy framework from black iron to king] first chapter - detailed explanation of 10000 character blog posts (recommended Collection)
Nutch2.1 using eclipse debug to store the build process in MySQL on the windows platform
WPF 测量字符串显示大小
Cmake development - Multi Directory Project
【Try to Hack】vulnhub narak
Analysis of the underlying architecture of spark storage system - spark business environment practice