惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Project Zero
Project Zero
V
Vulnerabilities – Threatpost
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
C
Cisco Blogs
A
Arctic Wolf
月光博客
月光博客
The GitHub Blog
The GitHub Blog
T
The Blog of Author Tim Ferriss
量子位
小众软件
小众软件
Latest news
Latest news
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Microsoft Security Blog
Microsoft Security Blog
T
The Exploit Database - CXSecurity.com
Security Latest
Security Latest
N
Netflix TechBlog - Medium
K
Kaspersky official blog
人人都是产品经理
人人都是产品经理
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
Y
Y Combinator Blog
P
Proofpoint News Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
M
MIT News - Artificial intelligence
T
Threat Research - Cisco Blogs
S
Schneier on Security
D
Docker
Scott Helme
Scott Helme
MyScale Blog
MyScale Blog
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
GbyAI
GbyAI
有赞技术团队
有赞技术团队
Google DeepMind News
Google DeepMind News
The Hacker News
The Hacker News
H
Help Net Security
Simon Willison's Weblog
Simon Willison's Weblog
J
Java Code Geeks
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tenable Blog
B
Blog
Know Your Adversary
Know Your Adversary
IT之家
IT之家

博客园 - ClawStudio

maven项目,httpclient jar包冲突 mysql5 数据库连接丢失问题,autoReconnect=true不起作用 博文阅读密码验证 - 博客园 org.springframework.web.method.HandlerMethod 与 org.springframework.messaging.handler.HandlerMethod 转换失败 dubbo常见错误 jvm实战-jvm调优 使用http load测试qps 博文阅读密码验证 - 博客园 jvm理论-字节码指令 博文阅读密码验证 - 博客园 jvm理论-字节码指令案例 jvm理论-class文件 博文阅读密码验证 - 博客园 jvm工具 jvm理论-运行时数据区 java nio-理解同步、异步,阻塞和非阻塞 真正的mybatis_redis二级缓存 博文阅读密码验证 - 博客园 博文阅读密码验证 - 博客园
jvm理论-常量池-string
ClawStudio · 2017-01-17 · via 博客园 - ClawStudio

字符串常量池-常量项(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://www.ciaoshen.com/2016/07/29/string/

http://tangxman.github.io/2015/07/27/the-difference-of-java-string-pool/

Java中几种常量池的区分

Thinking in Java 读书笔记:第十三章 - 字符串

https://www.zhihu.com/question/29884421/answer/113785601

http://rednaxelafx.iteye.com/blog/774673

http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html