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

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);

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

汪子熙 CSDN认证博客专家 前端框架 Node.js SAP
JerryWang,2007年从电子科技大学计算机专业硕士毕业后加入SAP成都研究院工作至今。Jerry是SAP社区导师,SAP中国技术大使。2020年5月下旬,Jerry做了脑部肿瘤的开颅切除手术,对编程和人生又有了新的感悟。
©️2020 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值