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

简介: 使用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:


image.pngWe can see the “i042416” is included in the constant pool:

image.pngThe java code String a = “i042416” is implemented via two lines of codes below:image.png(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.



image.png

image.pngAs a result, the following println gets true as result:

String aa1 = “i042416jerrywang”;

String aa2 = “jerrywang”;

String aa3 = “i042416” + “jerrywang”;

System.out.println(aa1 == aa3);


相关文章
|
1天前
|
存储 Java 索引
【JAVA基础篇教学】第十一篇:Java中字符串操作详解
【JAVA基础篇教学】第十一篇:Java中字符串操作详解
|
1天前
|
Java
代码实例演示Java字符串与输入流互转
代码实例演示Java字符串与输入流互转
|
2天前
|
安全 Java 编译器
Java中String、StringBuilder和StringBuffer的区别
Java中String、StringBuilder和StringBuffer的区别
|
2天前
|
Java
【Java多线程】分析线程加锁导致的死锁问题以及解决方案
【Java多线程】分析线程加锁导致的死锁问题以及解决方案
11 1
|
5天前
|
存储 缓存 安全
【 Java中String源码分析(JVM视角你不来看看?】
【 Java中String源码分析(JVM视角你不来看看?】
10 0
|
9天前
|
传感器 数据采集 网络协议
Java串口通信:从十六进制字符串到字节数组的正确转换与发送
Java串口通信:从十六进制字符串到字节数组的正确转换与发送
29 4
|
11天前
|
Java
在Java中,如何将字符串转换为浮点数?
【4月更文挑战第30天】在Java中,如何将字符串转换为浮点数?
16 0
|
11天前
|
Java
Java String类型转换成Date日期类型
Java String类型转换成Date日期类型
|
11天前
|
Java 索引
Java String应用与开发
Java String应用与开发
21 0
|
1月前
|
Java API 索引
Java基础—笔记—String篇
本文介绍了Java中的`String`类、包的管理和API文档的使用。包用于分类管理Java程序,同包下类无需导包,不同包需导入。使用API时,可按类名搜索、查看包、介绍、构造器和方法。方法命名能暗示其功能,注意参数和返回值。`String`创建有两种方式:双引号创建(常量池,共享)和构造器`new`(每次新建对象)。此外,列举了`String`的常用方法,如`length()`、`charAt()`、`equals()`、`substring()`等。
16 0