String.intern()方法小记

关于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);
    }

 

上一篇:Android webView 的一个坑,字体怎么突然变小了,android网络开发技术实战详解


下一篇:FindLetter 类——查找文件中特定的字符,每一行开头为某一个字符,则跳过