ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

String.intern()方法小记

2021-01-25 17:58:06  阅读:146  来源: 互联网

标签:code String System intern println 小记 out


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

    /**
     * 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);
    }

 

标签:code,String,System,intern,println,小记,out
来源: https://blog.csdn.net/BruceChao5211/article/details/113126407

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有