字符串常量区

  Java设计者为了提高字符串的性能,设计了字符串常量池。在方法区中。

  JDK 1.8 中,已经把字符串常量池从方法区的运行时常量池中分离到堆中了。字符串常量池中存储的就是对象。

参考资料:
JDK1.8版本java字符串常量池里存的是String对象还是引用?

利用一个题目巩固一下:

String name = new String(“A” + ”B”);

创建了几个对象:

  1. 三个字符串常量:“A”,“B”,“AB”(常量池对象)
  2. 一个对象 (堆对象)
  3. 一个引用

tips: 常量池中先看是否有该字符串对象,没有就创建,有就返回对象引用。

附 intern() 方法的 api 文档注释:

     * When the intern method is invoked, if the pool already contains a
     * string equal to this {@code String} object as determined by
     * the {@link #equals(Object)} method, then the string from the pool is
     * returned. Otherwise, this {@code String} object is added to the
     * pool and a reference to this {@code String} object is returned.
     * <p>

intern方法参考资料:
jdk1.8中String的intern方法简单介绍

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值