博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
String类的intern方法随笔
阅读量:4077 次
发布时间:2019-05-25

本文共 1855 字,大约阅读时间需要 6 分钟。

最近看书看到了String的intern()方法,特此记录以下

首先,来看源码中的定义:

/** * 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. * 

* It follows that for any two strings {@code s} and {@code t}, * {@code s.intern() == t.intern()} is {@code true} * if and only if {@code s.equals(t)} is {@code true}. * @return a string that has the same contents as this string, but is * guaranteed to be from a pool of unique strings. */public native String intern();

上面的注释翻译过来大体意思为:当inter()方法被执行时,如果常量池中已经有一个与此String字符相等的对象的时候,则返回常量池中的String对象,否则将此String对象加入常量池中并返回。当且仅当s.equals(t)时s.intern() == t.intern()成立。

由于是native方法,看不见具体实现,这里就通过代码来测试,接下来来看如下代码:

public class StringTest {    private static final String SOURCE_STRING = "aaa";    @Test    public void testIntern() {        String src = new String("aaa");        String desc = new String("aaa");        System.out.println(desc == src);        String newSrc = src.intern();        String newDesc = desc.intern();        System.out.println(src == newSrc);        System.out.println(newDesc == newSrc);        System.out.println(newSrc == SOURCE_STRING);        System.out.println(newDesc == SOURCE_STRING);    }}

运行结果:

falsefalsetruetruetrue

接下来来逐条解释:

  1. src==desc这个为false很容易理解,src和desc都是指向的堆中的不同的两个对象,只是这两个对象的值都是aaa,但是他们有不同的内存地址,所以==符号做比对的话肯定为false;
  2. src==newSrc这一步中,src指向的是堆中值为aaa的这个对象;根据intern方法的定义可知,String newSrc = src.intern();这一步操作会检查常量池,其中已有值为aaa对象,即为SOURCE_STRING,之后将newStr的引用指向SOURCE_STRING,所以newSrc指向的是常量池中值为aaa的这个对象,所以src==newSrc结果为false;
  3. newDesc == newSrc;这个同2,都指向的是常量池中值为aaa的SOURCE_STRING对象,故结果相同;
  4. 后面两条是为了验证2所说,结果如运行结果。

我的博客:blog.scarlettbai.com

转载地址:http://aesni.baihongyu.com/

你可能感兴趣的文章
【leetcode】Linked List Cycle (python)
查看>>
【leetcode】Sum Root to leaf Numbers
查看>>
如何成为编程高手
查看>>
本科生的编程水平到底有多高
查看>>
从mysql中 导出/导入表及数据
查看>>
几个常用的Javascript字符串处理函数 spilt(),join(),substring()和indexof()
查看>>
9、VUE面经
查看>>
Golang 数据可视化利器 go-echarts ,实际使用
查看>>
mysql 跨机器查询,使用dblink
查看>>
Jenkins + Docker + SpringCloud 微服务持续集成 - 单机部署(二)
查看>>
C#控件集DotNetBar安装及破解
查看>>
Winform多线程
查看>>
C# 托管与非托管
查看>>
Node.js中的事件驱动编程详解
查看>>
nodejs内存控制
查看>>
MongoDB 数据文件备份与恢复
查看>>
MongoDB数据库插入、更新和删除操作详解
查看>>
MongoDB文档(Document)全局唯一ID的设计思路
查看>>
mongoDB简介
查看>>
Redis持久化存储(AOF与RDB两种模式)
查看>>