Jvm theory-constant pool-string, jvm constant pool-string

Source: Internet
Author: User

Jvm theory-constant pool-string, jvm constant pool-string

String constant pool-constant item (Cp_info) Structure

CONSTANT_String_info {u1 tag = 8; u2 string_index; // store CONSTANT_Utf8_info pointer} CONSTANT_Utf8_info {u1 tag = 1; u2 length; // byte array length u1 bytes [length]; // UTF-8 encoded byte array}

 

Case 1

Public class StringDemo {private String s1 = "String S1 ";}

Javac StringDemo. java

Javap-verbose StringDemo. class

#15: CONSTANT_Utf8_info struct

CONSTANT_Utf8_info {u1 tag = 1; u2 length; // The length of the byte array u1 bytes [length]; // The byte array encoded with utf8}

#2: CONSTANT_String_info struct

CONSTANT_String_info {u1 tag = 8; u2 string_index; // storage #15}

 

Case 2

public class StringDemo {    private String s1="MyStr1";    private String s2="MyStr1";}

Javac StringDemo. java

Javap-verbose StringDemo. class

 

Case 3

public class StringDemo {    private String s1= new String("MyStr1");}

 

Case 4

public class StringDemo20 {    private String s1="MyStr1";    private String s2="MyStr1";    private String s3="My"+"Str1";    private String s01="My";    private String s02="Str1";    private String s4 = s01+s02;    private String s5 = s4.intern();        private static String str1="MyStr1";    private static String str2="MyStr1";    private static String str3="My"+"Str1";    private static String str01="My";    private static String str02="Str1";    private static String str4 = str01+str02;    private static String str5 = str4.intern();        private static final String fstr1="MyStr1";    private static final String fstr2="MyStr1";    private static final String fstr3="My"+"Str1";    private static final String fstr01="My";    private static final String fstr02="Str1";    private static final String fstr4 = fstr01+fstr02;    private static final String fstr5 = fstr4.intern();        class InnerDemo{        private String s1="MyStr1";        private String s2="MyStr1";        private String s3="My"+"Str1";        private String s01="My";        private String s02="Str1";        private String s4 = s01+s02;        private String s5 = s4.intern();                private static final String ifstr1="MyStr1";        private static final String ifstr2="MyStr1";        private static final String ifstr3="My"+"Str1";        private static final String ifstr01="My";        private static final String ifstr02="Str1";        private static final String ifstr4 = ifstr01+ifstr02;        //private static final String ifstr5 = ifstr4.intern();    }        public static void main(String[] args){        StringDemo20 stringDemo = new StringDemo20();         System.out.println("s1==s2:"+(stringDemo.s1==stringDemo.s2));        System.out.println("s1==s3:"+(stringDemo.s1==stringDemo.s3));        System.out.println("s1==s4:"+(stringDemo.s1==stringDemo.s4));//false        System.out.println("s1==s5:"+(stringDemo.s1==stringDemo.s5));                System.out.println("s1==str1:"+(stringDemo.s1==StringDemo20.str1));        System.out.println("s1==str3:"+(stringDemo.s1==StringDemo20.str3));        System.out.println("s1==str4:"+(stringDemo.s1==StringDemo20.str4));//false        System.out.println("s1==str5:"+(stringDemo.s1==StringDemo20.str5));                System.out.println("s1==fstr1:"+(stringDemo.s1==StringDemo20.fstr1));        System.out.println("s1==fstr3:"+(stringDemo.s1==StringDemo20.fstr3));        System.out.println("s1==fstr4:"+(stringDemo.s1==StringDemo20.fstr4));        System.out.println("s1==fstr5:"+(stringDemo.s1==StringDemo20.fstr5));         System.out.println("s1==innerdemo.fstr1:"+(stringDemo.s1==InnerDemo.ifstr1));        System.out.println("s1==innerdemo.fstr3:"+(stringDemo.s1==InnerDemo.ifstr3));        System.out.println("s1==innerdemo.fstr4:"+(stringDemo.s1==InnerDemo.ifstr4));    }}

Javap-verbose stringdemow.class> d:/1.txt

 

 

We can see that str1, str2, str3, and str5 are all ldc #22.

We can see that str1, str2, and str3 are all ldc #22. During the running of s5, put it into the runtime pool.

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.