再谈String

一、字符串常量池

1.1 创建对象的思考

下面创建String对象的方式相同吗?

public static void main(String[] args) {
   String s1 = "hello";
   String s2 = "hello";
   String s3 = new String("hello");
   String s4 = new String("hello");
   System.out.println(s1 == s2); // true
   System.out.println(s1 == s3); // false
   System.out.println(s3 == s4); // false
}

上述创建方式类似,为什么s1和s2引用的是同一个对象,而s3和s4不是呢?
在Java程序中,类似于:1, 2, 3,3.14,“hello”等字面类型的常量经常频繁使用,为了使程序的运行速度更快、更节省内存,Java为8种基本数据类型和String类都提供了常量池
“池” 是编程中的一种常见的, 重要的提升效率的方式, 我们会在未来的学习中遇到各种 “内存池”, “线程池”, “数据库连接池”……
为了节省存储空间以及程序的运行效率,Java中引入:

  1. Class文件常量池:每个.Java源文件编译后生成.Class文件中会保存当前类中的字面常量以及符号信息
  2. 运行时常量池:在.Class文件被加载时,.Class文件中的常量池被加载到内存中称为运行时常量池,运行时常量池每个类都有一份
  3. 字符串常量池

1.2 字符串常量池(StringTable)

字符串常量池在JVM中是StringTable类,实际是一个固定大小的HashTable(一种高效用来进行查找的数据结构),不同JDK版本下字符串常量池的位置以及默认大小不同:
在这里插入图片描述

1.3 再谈String 对象创建

双引号引起来的就是字符串,都会放在常量中,创建字符串时,先去常量池中查找是否有当前这个字符串,常量池没有当前字符串就创建,有就不再创建

  1. 直接使用字符串常量进行赋值
public static void main(String[] args) {
   String s1 = "hello";
   String s2 = "hello";
   System.out.println(s1 == s2); // true
}

在这里插入图片描述
2. 通过new创建String对象

public static void main(String[] args) {
   String s1 = "hello";
   String s2 = "hello";
   String s3 = new String("hello");
   System.out.println(s1 == s2); // true
   System.out.println(s1 == s3); // false
}

在这里插入图片描述
结论:只要是new的对象,都是唯一的
使用常量串创建String类型对象的效率更高,而且更节省空间。用户也可以将创建的字符串对象通过 intern 方式添加进字符串常量池中。
3. intern方法
将调用该方法的对象所指对象入池,如果常量池中存在,就不入池

public static void main(String[] args) {
   char[] ch = new char[]{'h', 'e', 'l','l','o'};
   String s1 = new String(ch); // s1对象不在常量池中
   //s1.intern(); 调用之后,会将s1对象的引用放入到常量池中
   String s2 = "hello"; // "abc" 在常量池中存在了,s2创建时直接用常量池中"abc"的引用
   System.out.println(s1 == s2);
}

intern调用前
在这里插入图片描述
intern调用后
在这里插入图片描述

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值