zoukankan      html  css  js  c++  java
  • 干货 | 深入分析 string.intern() 方法

    640?wx_fmt=jpeg

                                                

    首先我们来看一段代码:

    public class InternTest {
      
      public static void main(String[] args) {
        String str1 = new String("hello") + new String("world");
        str1.intern();
        String str2 = "helloworld";
        System.out.println(str1 == str2);//true
        System.out.println(str1.intern() == str2);//true
      }
    }

    大家对上面代码的运行结果一定很疑惑吧,第二个为true可以理解,因为intern的返回值本来就是该常量在常量池中的地址,但是为什么第一个也是true呢?
    那么是什么导致的呢?答案就是这个intern的实现方式。
    在jdk1.7之前的版本,调用这个方法的时候,会去常量池中查看是否已经存在这个常量了,如果已经存在,那么直接返回这个常量在常量池中的地址值,如果不存在,则在常量池中创建一个,并返回其地址值。
    但是在jdk1.7以及之后的版本中,常量池从perm区搬到了heap区。intern检测到这个常量在常量池中不存在的时候,不会直接在常量池中创建该对象了,而是将堆中的这个对象的引用直接存到常量池中,减少内存开销。
    所以调用第二行代码的时候,就是将heap中的地址值存放到常量池中,多以第三行代码获取的就是该字符串在heap中的地址值。
    如果我们将第二行代码和第三行代码的顺序调换:
    public class InternTest {
      
      public static void main(String[] args) {
        String str1 = new String("hello") + new String("world");
        String str2 = "helloworld";
        str1.intern();
        System.out.println(str1 == str2);//false
        System.out.println(str1.intern() == str2);//true
      }
    }

    public class InternTest {
      
      public static void main(String[] args) {
        String str1 = new String("java");
        str1.intern();
        String str2 = "java";
        System.out.println(str1 == str2);//false
        System.out.println(str1.intern() == str2);//true
      }
    }

    这个结果其实很好理解,在第一行代码执行的时候,会在heap中创建一个对象,并且回去常量池中查看该字符串是否已经存在,如果不存在,那么久创建一个。所以第二行代码可以说是没什么作用的。

    作者:Mazin

    来源:my.oschina.net/u/3441184/blog/887152

    - END -
    推荐阅读:

  • 相关阅读:
    How to install VXDIAG Honda, Toyota and JLR SDD software
    16% off MPPS V16 ECU tuning tool for EDC15 EDC16 EDC17
    Cummins INSITE locked and ask for verification code
    How to use BMW Multi Tool 7.3 to replace lost key for BMW X1
    Bleed Brake Master Cylinder with Intelligent Tester IT2
    Porsche Piwis Tester II “No VCI has been detected”,how to do?
    Creader VIII VS. Creader VII+
    How to solve GM MDI cannot complete the installation
    汽车OBD2诊断程序开发 (原文转载,思路很清晰!)
    汽车节温器单片机开发思路
  • 原文地址:https://www.cnblogs.com/java-stack/p/11952023.html
Copyright © 2011-2022 走看看