博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jvm理论-常量池-string
阅读量:6273 次
发布时间:2019-06-22

本文共 3710 字,大约阅读时间需要 12 分钟。

字符串常量池-常量项(cp_info)结构

CONSTANT_String_info{    u1 tag=8;    u2 string_index;//存放 CONSTANT_Utf8_info 指针}CONSTANT_Utf8_info{  u1 tag=1;  u2 length;//字节数组长度  u1 bytes[length];//使用utf8编码后的字节数组}

 

案例1

public class StringDemo {    private String s1="字符串S1";}

javac StringDemo.java

javap -verbose StringDemo.class

#15:CONSTANT_Utf8_info结构体

CONSTANT_Utf8_info{    u1 tag=1;    u2 length;//字节数组长度    u1 bytes[length];//使用utf8编码后的字节数组}

#2:CONSTANT_String_info结构体

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

 

 案例2

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

javac StringDemo.java

javap -verbose StringDemo.class

 

 案例3

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

 

案例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 StringDemo20.class >d:/1.txt

 

 

可以看到 str1、str2、str3、str5都是 ldc #22。

 可以看到 str1、str2、str3都是 ldc #22。s5在运行期间,放入运行时常量池。

 

推荐:

转载地址:http://hzlpa.baihongyu.com/

你可能感兴趣的文章
直接调用VS.net2005中的配置界面
查看>>
程序员的自我修养五Windows PE/COFF
查看>>
关于字符集,编码格式,大小端的简单总结
查看>>
js string 转 int Number()
查看>>
课堂练习:ex 4-20
查看>>
20155328 2016-2017-2 《Java程序设计》 第8周学习总结
查看>>
python操作redis--string
查看>>
echarts图表初始大小问题及echarts随窗口变化自适应
查看>>
Inherits、CodeFile、CodeBehind的区别
查看>>
创建一个SimpleDlg
查看>>
使用XML生成菜单
查看>>
udp,tcp对于socket的写法
查看>>
第二周个人赛
查看>>
推断Windows版本号新方法
查看>>
2017-4-18 ADO.NET
查看>>
RSuite 一个基于 React.js 的 Web 组件库
查看>>
技术博客网址收藏
查看>>
python 金融分析学习
查看>>
授人以渔不如授人以鱼
查看>>
matlab练习程序(图像Haar小波变换)
查看>>