search

Home  >  Q&A  >  body text

Java String的intern方法

String str1 = new StringBuilder("a").append("b").toString();
        System.out.println(str1.intern() == str1);
        String str2 = new StringBuilder("c").toString();
        System.out.println(str2.intern() == str2);

为什么输出结果是:
true
false

大家讲道理大家讲道理2890 days ago427

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-04-17 17:51:54

    String str1 = new StringBuilder("a").append("b").toString();  //1
    System.out.println(str1.intern() == str1);                    //2
    String str2 = new StringBuilder("c").toString();              //3
    System.out.println(str2.intern() == str2);                    //4

    There is an area called String constant pool in the Java memory model, which stores string constants.

    First of all, when the jdk version is less than or equal to 1.6, the result of executing the above code will be

    false
    false

    When the jdk version is greater than 1.6, the execution result of the above code is

    true
    false

    The reason for the above two different results is that jvm implements the intern() method differently.

    In jdk1.6 and before, call intern()

    If there is no string with equal value in the constant pool, jvm will copy a string to the creation pool and return the string in the constant pool.

    In jdk1.7 and later, call intern()

    If there is no string with equal value in the constant pool, jvm just records the reference of the current string in the constant pool and returns the reference of the current string.

    Next, we will explain why the above code will get a true and a false in jdk1.7 and above. true一个false

    当存在上述代码的类被JVM加载时,字面值常量 a, b, c 就会被加载到 string 常量池中,当执行str1.intern()时,由于常量池中并不存在字符串ab,jvm 会在常量池中记录str1的引用,并返回str1的引用,因此,第2行代码的输出为true

    str2使用字面值常量 c 构造了一个新的字符串,该字符串的引用和常量池中字面值c字符串的引用不相同,当调用str2.intern()时, 常量池中已经存在了c,jvm直接返回常量池中的引用,该引用不同于重新构造的str2,因此第4行代码的输出为false

    When the class containing the above code is loaded by the JVM, the literal constants a, b, c will be loaded into the string constant pool , when executing str1.intern(), since the string ab does not exist in the constant pool, jvm will record str1 in the constant pool The reference of str1 is returned. Therefore, the output of line 2 is true. 🎜 🎜str2 uses the literal constant c to construct a new string. The reference of the string is the same as the literal value c string in the constant pool. The references are not the same. When str2.intern() is called, c already exists in the constant pool, and jvm directly returns the reference in the constant pool, which is different from the reconstruction str2, so the output of line 4 is false. 🎜

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:51:54

    A String object calls intern()方法会先从常量池中找到equals该对象的常量并返回,若找不到就在常量池中增加一个equals该对象的常量,并返回该常量的应用。
    System.out.println(str1.intern() == str1);输出为true,是因为str1经过append操作后,变成一个常量池中常量的引用。
    System.out.println(str2.intern() == str2);输出为false,是因为str2是一个变量的引用,不在常量池中。
    因此,我们要判断str.intern() == strfalse还是true,主要看str是不是常量池中的常量,如果是结果就是true,否则就过就是false.

    reply
    0
  • Cancelreply