Java – string constant pool and intern

I've tried to understand the concepts of string constant pool and Inter in recent days. After reading many articles, I understand some parts of it, but I'm still confused about the following points: –

1. String a = "ABC" this will create an object in the string constant pool, but does the following code line create the object "XYZ" in the string constant pool? String B = ("XYZ") toLowerCase()

two

String c = "qwe"   
String d = c.substring(1)    
d.intern()   
String e = "we"

If the text "we" is added to the string constant pool during class loading, if so, why does d = = e result true, even if D does not point to the string constant pool

Solution

String pool is delaying loading If you call Intern () yourself before the string text, this is the version of the string that will enter the string pool If you don't call Intern () yourself, the string text will fill the string pool for us

Surprisingly, we can affect the string pool before the constant pool; As shown in the following code snippet

To understand why two code fragments behave differently, it is important to be clear

> the constant pool is not the same as the string pool. That is, the constant pool is a part of the class files stored on disk, and the string pool is a runtime cache filled with strings. > And according to the Java language specification jls-3.10 5. The reference string text does not directly reference the constant pool; Character literals populate the string pool from the constant pool if and only if there are no values in the string pool

That is, the life cycle of the string object from the source file to the runtime is as follows:

>At compile time, the compiler puts it into the constant pool and stores it in the generated class file (each class file has a constant pool) > the JVM loads the constant pool when the class is loaded > the string created from the constant pool is added to the string pool at run time, Because the calling Intern (if the equivalent string already exists, if the string already exists, the string in the string pool will be used) JVM spec 5.1 – the run time constant pool. > Intern can occur either explicitly by manually calling intern() or implicitly by referencing string literals such as "ABC" jls-3.10.5

The behavior difference between the following two code snippets is due to the explicit call to intern () before the implicit call to the interior occurs through the string literal

For clarity, here are two actions discussed in the comments on this answer:

String c = "qwe";   // string literal qwe goes into runtime cache
    String d = c.substring(1); // runtime string "we" is created
    d.intern();         // intern "we"; it has not been seen 
                        // yet so this version goes into the cache
    String e = "we";    // Now we see the string literal,but
                        // a value is already in the cache and so 
                        // the same instance as d is returned 
                        // (see ref below)

    System.out.println( e == d );  // returns true

The following is our practice after using string Text:

String c = "qwe";   // string literal qwe goes into runtime cache
    String d = c.substring(1); // runtime string "we" is created
    String e = "we";    // Now we see the string literal,this time
                        // a value is NOT already in the cache and so 
                        // the string literal creates an object and
                        // places it into the cache
    d.intern();         // has no effect - a value already exists
                        // in the cache,and so it will return e

    System.out.println( e == d );  // returns false
    System.out.println( e == d.intern() );  // returns true
    System.out.println( e == d );  // still returns false

The following is a key part of JLS. The declaration is actually an intern called for string text

The JVM specification covers the details of the runtime representation of the constant pool loaded from the class file, and it interacts with interns

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
分享
二维码
< <上一篇
下一篇>>