理解String.intern()

2019-10-03  本文已影响0人  Okami_
/**
     * 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();

String设计成不可变的原因

创建字符串

        String s1 = "hello";  // 在常量池中创建常量
        String s2 = "hello";  // 直接返回已经存在的常量
        String s1 = "hello";
        String s2 = "hello";
        System.out.println(s1 == s2);        // true
        System.out.println(s1.equals(s2));    // true
        String s1 = new String("hello");  // 在堆上创建字符串对象
        s1.intern();    // 在常量池中创建对象的引用
        String s2 = "hello";  // 常量池中存在该常量,直接返回该引用指向的堆空间对象
        String s1 = new String("hello").intern();
        String s2 = "hello";
        System.out.println(s1 == s2);      // true
        System.out.println(s1.equals(s2));      // true
        String s1 = new String("hello");
        String s2 = new String("hello");
        System.out.println(s1 == s2);      // false
        System.out.println(s1.equals(s2));    // true
        String s1 = new String("hello").intern();
        String s2 = new String("hello").intern();
        System.out.println(s1 == s2);    // true
        System.out.println(s1.equals(s2));    // true
        String s1 = new String("hello").intern();
        String s2 = "hello";
        System.out.println(s1 == s2);    // true
        System.out.println(s1.equals(s2));  // true
    /**
     * Returns the string representation of the {@code Object} argument.
     *
     * @param   obj   an {@code Object}.
     * @return  if the argument is {@code null}, then a string equal to
     *          {@code "null"}; otherwise, the value of
     *          {@code obj.toString()} is returned.
     * @see     java.lang.Object#toString()
     */
    public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }
        String s1 = String.valueOf("hello");
        String s2 = "hello";
        System.out.println(s1 == s2);    // true
        System.out.println(s1.equals(s2));    // true
        String s1 = String.valueOf("hello");
        String s2 = String.valueOf("hello");
        System.out.println(s1 == s2);    // true
        System.out.println(s1.equals(s2));    // true
        String s1 = String.valueOf("hello");
        String s2 = new String("hello").intern();
        System.out.println(s1 == s2);    // true
        System.out.println(s1.equals(s2));    // truee
上一篇 下一篇

猜你喜欢

热点阅读