前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用javap工具分析Java String(字符串)操作

使用javap工具分析Java String(字符串)操作

作者头像
Jerry Wang
发布2020-07-06 23:43:12
2690
发布2020-07-06 23:43:12
举报

Created by Jerry Wang, last modified on Oct 06, 2016

Put this line into class main method: String a = “i042416”; And decompile the .class file using javap:

We can see the “i042416” is included in the constant pool:

The java code String a = “i042416” is implemented via two lines of codes below:

(1) When the class is loaded by JVM, the string “i042416” is represented by #16. When instruction ldc #16 is called for the first time, the native method StringTable::intern will be called to generate char[], and store the reference into StringTable and constant pool. When the ldc #16 is called subsequently, the reference #16 is directly returned from constant pool.

(2) Instruction astore_1 stores the reference of “i042416” to local variable table. Test via these lines instead: String aa1 = “i042416”; String aa2 = “jerrywang”; String aa3 = “i042416” + “jerrywang”; We can see for line aa3, the string concatenation is done in compilation time.

As a result, the following println gets true as result: String aa1 = “i042416jerrywang”; String aa2 = “jerrywang”; String aa3 = “i042416” + “jerrywang”; System.out.println(aa1 == aa3);

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-07-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Created by Jerry Wang, last modified on Oct 06, 2016
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档