0
点赞
收藏
分享

微信扫一扫

String s = new String(“java“) 到底创建了几个对象

灯火南山 2022-01-11 阅读 13

在读《深入理解Java虚拟机:JVM高级特性与实战(第3版)》看到这样一段话:

按理说这两个结果不应该一样吗?为什么会出这种题呢?

 

为了证明我在思考ing,我特地的试了一下

为什么?为什么?为什么?为什么是trueflase

 根据书中吧啦吧啦吧啦说了一大堆,意思就是想让读者知道,字符串常量池jdk1.6jdk1.7及以上存放的位置变啦,以前放到永久代的,以后就放在java堆上了。jdk1.6intern()的方法首次遇到会把字符串复制一份到有永久代,而jdk1.7及以上的只需要在常量池里记录一下首次出现的实例引用。"只需要在常量池里记录一下首次出现的实例引用"说白了就是常量池记录第一次出现这个字符串的地址。其实我这地方有个好奇,那不就是这个对象永久不会被垃圾收集器收集了嘛?当然我也不敢问。。。

既然都说到这了,那就一起看看intern()方法吧!(jdk1.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方法时,如果池中已经包含一个等于此对象的字符串,此方法,则从池中取出字符串返回。否则,此对象地址将添加到池,并返回对此对象的引用。 额,和上面一个意思。

叽叽歪歪说了半天,我知道第一个是true,我是问你第二个为什么是false

其实在《如何理解《深入理解java虚拟机》第二版中对String.intern()方法的讲解中所举的例子?》

呵呵哒,原来在初始化的时候已经把java这个字符串放到常量池中了。

举个例子让我怀疑了人生,哎。。。


哎,别先,如果我发现了一个面试题,String s = new String("s")证明他是创建两个对象的了,哈哈哈哈。。。

 

public static void main(String[] args) {
    String s = new String("s");
    System.out.println(s.intern() == s);
}

结果是false,那不就是创建了两个对象吗?第一个是new String("s"),第二个是常量池的s。哈哈哈哈。

小编小编,那你怎么证明java启动的时候常量池里面没有s呢?毕竟java这个字符串一开始也存在常量池啊!

最后。。。

String s = new String("java")它创建了一个对象,这次没人反驳我了把。哈哈哈哈~

举报

相关推荐

0 条评论