Java – try creating with strings

I found an interesting case when I used a string to create and check its hash code

In the first case, I use the copy constructor to create

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        String s1 = new String("myTestString");

        String s3 = s1.intern();

        System.out.println("S1: " + System.identityHashCode(s1) + "  S3:"
                + System.identityHashCode(s3));
    }


}

The output of the above code is:

This is the expected output because the internship string selects the reference from the string pool and S1 selects the reference of the new object So their identity hash codes are different

Now, if I use the char array to create a string, I will see some strange behavior:

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        char[] c1 = { 'm','y','T','e','s','t','S','r','i','n','g' };

        String s5 = new String(c1);

        String s6 = s5.intern();

        System.out.println("S5: " + System.identityHashCode(s5) + "  S6:"
                + System.identityHashCode(s6));
    }

}

The output of the above code is:

This is an unexpected output How do interned string and new string objects have the same identityhashcode?

Any ideas?

Solution

In the first case, the myteststring text is in the pool before you call the intern, while in the second case, it is not. Your string S5 is directly in the pool

If we look at your example step by step, the following happens:

> String s1 = new String(“myTestString”); => Use string text to create a string myteststring (let's call it S0) in the pool, and also create a new string S1, which is not in the pool. > String s3 = s1. intern(); => Check whether there is an equivalent string in the pool and find S0 Now S3 and S0 refer to the same instance (that is, S3 = = S0 is true, but S1! = S0)

In your second example:

> String s5 = new String(c1); Create a new string that is not in the pool > string S6 = S5 intern(); Check whether myteststring is in the pool, but it cannot be found, so the call to the intern will create a new string reference in the pool, which refers to the same string as S5 So S6 = = S5 is true

Finally, you can run these two programs to confirm my explanation (the second print is true three times):

public static void main(String[] args) {
    String s1 = new String("myTestString");
    String s3 = s1.intern();
    System.out.println("myTestString" == s1);
    System.out.println(s3 == s1);
    System.out.println("myTestString" == s3);
}

public static void main(String[] args) {
    String s1 = new String(new char[] {'m','g'});
    String s3 = s1.intern();
    System.out.println("myTestString" == s3);
    System.out.println("myTestString" == s1);
    System.out.println(s3 == s1);
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>