String.intern()方法小记

7 篇文章 0 订阅
3 篇文章 0 订阅

关于String.intern()方法,源码中有很完善的描述(jdk_1.8)

    /**
     * Returns a canonical representation for the string object.
     * <p>
     * A pool of strings, initially empty, is maintained privately by the
     * class {@code String}.
     * <p>
     * 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>
     * 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}.
     * <p>
     * All literal strings and string-valued constant expressions are
     * interned. String literals are defined in section 3.10.5 of the
     * <cite>The Java&trade; Language Specification</cite>.
     *
     * @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();

其中重点是第三段话,大概意思是:当intern方法执行的时候,如果常量池中已经存在与给定字符串内容相同的字符串,则返回常量池中已存在的这个字符串;否则,给定字符串就会被添加到常量池然后返回它的引用。

测试用例:

public class StringDemo {
    public static void main(String[] args) {
        // 当前构造方法并不会将abcm加入常量池
        String a = new String(new char[]{'a', 'b', 'c', 'm'});
        // 调用a.intern()方法时,会将调用方a的地址加入常量池,并返回这个引用地址
        // 之后所有abcm对象调用intern方法的时候返回的都是刚加入常量池的a的引用地址
        System.out.println(a.intern() == a); // true
        String b = "abcm";
        System.out.println(a.intern() == b); // true
        System.out.println(b.intern() == b); // true
        System.out.println(b.intern() == a); // true
        System.out.println(a == b); // true
    }
}

进行修改,注释掉第一行打印:

public class StringDemo {
    public static void main(String[] args) {
        // 当前构造方法并不会将abcm加入常量池
        String a = new String(new char[]{'a', 'b', 'c', 'm'});
        // 调用a.intern()方法时,会将调用方a的地址加入常量池,并返回这个引用地址
        // 之后所有abcm对象调用intern方法的时候返回的都是刚加入常量池的a的引用地址
        // System.out.println(a.intern() == a); 
        String b = "abcm";
        System.out.println(a.intern() == b); // true
        System.out.println(b.intern() == b); // true
        System.out.println(b.intern() == a); // false
        System.out.println(a == b); // false
    }
}

注释掉第一行打印后,在定义b之前就不会执行 a.intern() 方法,定义b之后所有 "abcm" 调用 intern 方法返回的都是b。

 

需要注意的是String的char[]构造方法并未使用到任何字符串,也不会操作字符串常量池

    public String(char value[]) {
        this.value = Arrays.copyOf(value, value.length);
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值