identityHashCode运行分析

identityHashCode

程序源代码如下:

public class Main11 {
    public static void main(String args[]) {
        String str=new String("苹果");
        System.out.println(System.identityHashCode(str.intern()));
        System.out.println(System.identityHashCode(str));
        modify(str);
        System.out.println(System.identityHashCode(str));
    }
    public static void modify(String s) {
        System.out.println(System.identityHashCode(s));
        s=s+"好吃";
        System.out.println(System.identityHashCode(s));
    }
}

运行结果如下:

460141958
1163157884
1163157884
1956725890
1163157884

程序分析:
将各程序与结果一一对应得到如下结果,逐个分析

  • System.identityHashCode
    返回给定对象的哈希码,该代码与默认的方法 hashCode() 返回的代码一样,无论给定对象的类是否重写 hashCode()。null 引用的哈希码为 0。
  • str.intern()
    String.intern()是一个Native方法,底层调用C++的 StringTable::intern方法实现。当通过语句str.intern()调用intern()方法后,JVM 就会在当前类的常量池中查找是否存在与str等值的String,若存在则直接返回常量池中相应Strnig的引用;若不存在,则会在常量池中创建一个等值的String,然后返回这个String在常量池中的引用。
    由于intern()操作每次都需要与常量池中的数据进行比较以查看常量池中是否存在等值数据,同时JVM需要确保常量池中的数据的唯一性,这就涉及到加锁机制,这些操作都是有需要占用CPU时间的,所以如果进行intern操作的是大量不会被重复利用的String的话,则有点得不偿失。由此可见,String.intern()主要适用于只有有限值,并且这些有限值会被重复利用的场景,如数据库表中的列名、人的姓氏、编码类型等。
 public static void main(String args[]) {
        String str=new String("苹果");
        System.out.println(System.identityHashCode(str.intern()));
        //460141958 JVM在常量池中查找了其等值数据并返回其哈希码
        System.out.println(System.identityHashCode(str));
        //1163157884 返回str的哈希码
        modify(str);
        System.out.println(System.identityHashCode(str));
        //1163157884 返回原本str的哈希吗,由于modify()未修改str的值
    }
    public static void modify(String s) {
        System.out.println(System.identityHashCode(s));
        //1163157884 先测试返回s的哈希码,可知哈希码与变量名无关,由内容决定
        s=s+"好吃";
        System.out.println(System.identityHashCode(s));
        //1956725890 返回更改之后的字符串S的哈希码
    }
posted @ 2021-07-09 20:31  SKPrimin  阅读(88)  评论(0编辑  收藏  举报