Java String constant pool summary, javastring

Source: Internet
Author: User

Java String constant pool summary, javastring

I recently interviewed a website construction Internet company in Guangzhou. At that time, the interviewer asked me if there were two strings: String a = "abc", String B = "abc "; whether the output a = B is true or false. I did not hesitate to answer "true" at the time, and then explained it according to the knowledge point of the String constant pool combined with the jvm memory model. However, he told me that "false" is the most basic problem. I was so arrogant that I discussed it with him for a long time. Later I found out that he was wrong. He said that the variables a and B exist in the stack, and the two references are different, but they point to the same content. After all, because he had been working for several years, I also questioned my knowledge points and accepted his answer. After I come back to the computer for practical verification, I am right. The details are as follows. First, we should first make three pieces of code:

Section 1:

public static void main(String[] args) {       String s1 = "hello";       String s2 = "hello";       String s3 = "he" + "llo";       String s4 = "hel" + new String("lo");       String s5 = new String("hello");       String s6 = s5.intern();       String s7 = "h";       String s8 = "ello";       String s9 = s7 + s8;      System.out.println(s1==s2);//true        System.out.println(s1==s3);//true        System.out.println(s1==s4);//false        System.out.println(s1==s9);//false        System.out.println(s4==s5);//false        System.out.println(s1==s6);//true    }

The result of running in jdk1.6, 1.7, and 1.8 is:

System. out. println (s1 = s2); // true
System. out. println (s1 = s3); // true
System. out. println (s1 = s4); // false
System. out. println (s1 = s9); // false
System. out. println (s4 = s5); // false
System. out. println (s1 = s6); // true

Section 2:

public static void main(String[] args) {        String s1 = new String("hello");        String intern1 = s1.intern();        String s2 = "hello";        System.out.println(s1 == s2);        String s3 = new String("hello") + new String("hello");        String intern3 = s3.intern();        String s4 = "hellohello";        System.out.println(s3 == s4);    }

The result of running in jdk1.6 is:

False, false

The result of running in jdk1.7 and 1.8 is:

False, true

Section 3:

public static void main(String[] args) {        String s1 = new String("hello");        String s2 = "hello";        String intern1 = s1.intern();        System.out.println(s1 == s2);        String s3 = new String("hello") + new String("hello");        String s4 = "hellohello";        String intern3 = s3.intern();        System.out.println(s3 == s4);    }

The result of running in jdk1.6 is:

False, false

The result of running in jdk1.7 and 1.8 is:

False, false

 

Code 1:

The final modifier of the String class. When a String variable is created literally, jvm will put the literal ("hello") in the String constant pool during compilation, when started by the Java program, it has been loaded into the memory. The character string constant pool has only one identical literal value. If there are other identical literal values, jvm returns this literal reference, if there is no literal, create the literal In the String constant pool and return its reference. Because the literal "hello" pointed by s2 already exists in the constant pool (s1 prior to s2), jvm returns the reference bound to this literal, so s1 = s2. The literal concatenation in s3 is actually "hello". jvm has optimized it during compilation, so s1 and s3 are equal. In s4, new String ("lo") generates two objects: "lo", "new String (" lo ")", and "lo" has a String constant pool, "new String (" lo ")" is in the heap. String s4 = "El" + new String ("lo") is essentially the addition of two objects, and the compiler will not optimize them, the added result is in the heap, while s1 is in the String constant pool, which is not equal. S1 = s9 works the same way. S4 = the result of adding both s5 is in the heap. Needless to say, they are definitely not equal. S5.intern () in s1 = s6 () the method can dynamically Add a string in the heap to the String constant pool during running (the content of the String constant pool is loaded when the program starts ), if the String constant pool contains the literal volume corresponding to this object, the reference of this literal volume in the String constant pool is returned. Otherwise, create and copy the literal to the String constant pool and return its reference. Therefore, s1 = s6 outputs true.

The second code:

In jdk1.6, The String constant pool is in the permanent zone and is completely independent from the heap. s1 points to the content in the heap, and s2 points to the content in the String constant pool. Of course, the two are different, s1.intern1 () adds the literal to the String constant pool. Because the String constant pool already exists, the unique reference of the literal is returned. If intern1 = s2, true is output.

The String constant pool in jdk1.7 and 1.8 has been transferred to the heap. It is part of the heap. The jvm designer made some modifications to intern (). When s3.intern () is executed, the jvm no longer copies the s3 literal to the String constant pool, but stores an s3 reference in the String constant pool. This reference points to the literal volume in the heap, when running to String s4 = "hellohello", it is found that the String constant pool already has a reference pointing to the literal volume in the heap, then this reference is returned, and this reference is s3. So s3 = s4 outputs true.

Code 3:

The output in JDK is false or false. The principle is the same as above.

In jdk1.7, 1.8, the output value is false, false, and s3 points to the heap content. then s4 = "hellohello" finds that the String constant pool does not have the literal value, and creates and returns the reference. S3 and s4 are one in the heap, and the other in the String constant pool. They are two independent references, so s3 = s4 outputs false.

 

05:44:23

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.