String常量池理解

0概述

运行环境:java version “1.8.0_05”(JDK 1.8)
自JDK 1.7 版本以后字符串的常量池已经从永久代(方法区)迁移到堆区。
Area: HotSpot
Synopsis: In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the String.intern() method will see more significant differences.
(具体见 http://www.oracle.com/technetwork/java/javase/jdk7-relnotes-418459.html)

1 实例分析

public class StringTest {
    public static void main(String[] args) {
        String str=new StringBuilder("hsc").append("test").toString();
        String strIntern=str.intern();
        String strNew=new StringBuilder("hsc").append("test").toString();
        String strNewIntern=strNew.intern();
        /**
         * 输出:true
         */
        System.out.println(str==strIntern);
        /**
         * 输出:true
         */
        System.out.println(strNewIntern==str);
        /**
         * 输出:false
         */
        System.out.println(strNewIntern==strNew);
    }
}

首先解释下intern()方法是干什么的?JDK源码中给出的注释可以看出:它返回一个来自符串池(常量池)且内容与此字符串相同的字符串。
当该方法被调用时,如果池已经包含一个等于此 String 对象的字符串(该对象由 equals(Object) 方法确定),则返回池中的字符串。否则,将此 String 对象添加到池中(自JDK1.7的intern()实现不会再复制实例,只是在 常量池中记录首次出现实例的引用),并且返回此 String 对象的引用。
也值得说明是:==就是用来比较值是否相等,str,strIntern,strNew,strNewIntern是引用变量(相当于C语言指针)比较引用变量值是否相等也就是比较其指向的地址是否相等。

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

这里写图片描述
分析如下:
第一次在堆区创建str:“hsctest”字符串
调用intern()方法,常量池没有改字符串,该字符串加入常量池(返回首次出现实例的引用)
第二次在堆区创建strNew:“hsctest”字符串
调用intern()方法,常量池已经有该字符串,直接返回引用。即:str:“hsctest”字符串

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值