Java: text strings

class A { 
class A { 

String s4 = "abc";

 static public void main(String[]args ) {

        String s1 = "abc";
        String s2 = "abc";
        String s3 = new String("abc");
        A o = new A();
        String s5 = new String("def");

        System.out.println("s1==s2 : " + (s1==s2));
        System.out.println("s1==s1.intern : " + (s1==s1.intern()));
        System.out.println("s1==s3 : " + (s1==s3));
        System.out.println("s1.intern==s3.intern : " + (s1.intern()==s3.intern()));
        System.out.println("s1==s4 : " + (s1==o.s4));
 }
}

Output:

s1==s2 : true
s1==s1.intern : true
s1==s3 : false
s1.intern==s3.intern : true
s1==s4 : true

My question:

1. "String S1 =" ABC "what happens? I think the string object is added to the pool in the class string as an internship string. Where is it placed?" Permanently generate "or just a member of the heap (as data) string class instance)?

2. What happens when "string S2 =" ABC "? I don't think any objects have been created. But does this mean that Java intepreter needs to search all intercepted strings? Will this cause any performance problems?

3. Seems string S3 = new string ("ABC") does not use interconnected string Why?

4. Will string S5 = new string ("def") create any new internship string?

Solution

At compile time, the literal representation is written to the constant pool section of the class file containing this code

When the class is loaded, the representation of string text in the constant pool of the class file is read and a new string object is created from it The string is then implemented and the reference to the embedded string is "embedded" in the code

At runtime, a reference to the previously created / instantiated string is assigned to S1 (no string or string will be created when this statement is executed.)

Yes But not when code is executed

It is stored in the permgen area of the heap (the string class has no static field. The string pool of the JVM is implemented in native code.)

Nothing happened while loading When the compiler creates a class file, it reuses the same constant pool entry for the text used the first time Therefore, this statement uses the same string reference as the previous statement

correct.

No and No The Java interpreter (or JIT compiled code) creates / embeds the same reference as the previous statement

It's more complicated than that The constructor call uses the interconnected string, then creates a new string and copies the characters of the interconnected string to the representation of the new string The newly created string is assigned to S3

Why? Because new is specified to always create new objects (see JLS), and the string constructor is specified to copy characters

Create a new internship string at load time (for "def"), and then create a new string object at run time, which is a copy of the internship string (see the previous text for details.)

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