Why does String.intern () behave differently in Oracle JDK 1.7?

Here is a java fragment:

public class TestIntern {
    public static void main(String[] argvs){
        String s1 = new StringBuilder("ja").append("va").toString();
        String s2 = new StringBuilder("go").append("lang").toString();
        System.out.println(s1 == s1.intern());
        System.out.println(s2 == s2.intern());
    }
}

And it behaves differently depending on different JDKs

in Oracle JDK 1.7, the output is:

false
true

in the output of OpenJDK 1.6 also:

false
true

but in Oracle JDK 1.6 the output is:

false
false

since the JavaDoc for this method String#internindicates

 * When the intern method is invoked, if the pool already contains a
 * string equal to this <code>String</code> object as determined by
 * the {@link #equals(Object)} method, then the string from the pool is
 * returned. Otherwise, this <code>String</code> object is added to the
 * pool and a reference to this <code>String</code> object is returned.
                           ~~~~
                             And what does *this* here mean? the string object
                             in the heap or in the string pool? if it returns 
                             object in the heap the out put should be:

                             true
                             true
                             otherwise should *always* be:

                             false
                             false
                             Am I right?

conclusion:

true
true

to be expected, but none of these JDKs do this. and why Oracle JDK1.6 gives:

false
false

as a result?

I think that in OracleJDK 1.7 and openJDK 1.6 there should be some kind of reserved row in the row pool, and what is it? Does the document contain all reserved lines? Really confused.

+4
source share
2 answers

s1.intern() s1 - String, . - String, intern() ; String, s1 , .

, Java -, , . , Oracle JDK 1.7 openJDK 1.6 "java", "golang".

+5

, , .

@Ted Post. , , , s1 == s1.intern(). Oracle JDK 1.7 OpenJDK, Oracle JDK 1.6, false, , s1 .

, Oracle JDK 1.6 false - , JDK 1.7. JDK 1.6 .

, java- Oracle JDK 1.6

import java.io.*;

public class TestIntern {
    public static void main(String[] argvs){
       String s1 = null; // string inputed
       String s2 = null; // string retrieved by string.intern() in this loop
       String s3 = null; // string retrieved by string.intern() in last loop
        while (true){
            System.out.println("Enter the string");
            BufferedReader br = new BufferedReader(
                                     new InputStreamReader(System.in));

            try {
                s1 = br.readLine();
            }catch (IOException ex){
                System.out.println("IOException caught, just exit");
                System.exit(1);
            }

            s3 = s2;          //<- s3 is referring to  the string obj
                              //which retrieved from pool by last exec of loop

            s2 = s1.intern(); //<- s2 now referring to the string 
                              //obj retrieved from the string pool

            // is s1 == s2 ? // this loop
            if (s1 == s2){
                System.out.println("s1 == s2 they are same string obj");

            }else{
                System.out.println("s1 != s2 they are NOT same string obj");
            }

            // is s2 == s3 ? compared from this loop retrieved(s2) 
            //                               and last loop retrieved(s3)
            if(s2 == s3){
                System.out.println("s2 == s3 they are same string obj");
            }else{
                System.out.println("s2 != s3 they are NOT same string obj");
            }
        }
    }
}

, , s2 s3, stdin .

-, aaa , s2 s3 :

-> ~/Downloads/jdk1.6.0_45/bin/java TestIntern 
Enter the string
aaa
s1 != s2 they are NOT same string obj
s2 != s3 they are NOT same string obj
Enter the string
aaa
s1 != s2 they are NOT same string obj
s2 == s3 they are same string obj
Enter the string

, string.inter() aaa , , br.readLine(), . .

0

Source: https://habr.com/ru/post/1570250/


All Articles