欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Class文件结构-常量池

程序员文章站 2022-07-05 08:00:10
常量池里存放:1.字面量(Literal) • 文本字符串 • 声明为final的常量值(final的8种基本类型) • 非final的基本类型也可能进(doublefloatlong)2.符号引用(Symbolic References) • 类和接口的全限定名(Fully Qualified N ......

常量池里存放:
1.字面量(literal)
• 文本字符串
• 声明为final的常量值(final的8种基本类型)
• 非final的基本类型也可能进(doublefloatlong)
2.符号引用(symbolic references)
• 类和接口的全限定名(fully qualified name)
• 字段的名称和描述符(descriptor)
• 方法的名称和描述符
备注:
1. constant_pool_count=1表示常量池中有0个常量项
2. 表的第一个字节表示常量类型(tag)

常量项结构总表

Class文件结构-常量池

 示例:

package zero.desk.constantpool;

public class constantpool extends c implements a,b{
private string str = "test string";
private final int a = 10;
private final long b = 10;
private final long bb = 100;
private int c = 11;
private float d = 12f;
private float e = 12f;
private double ee = 12f;

private int m;

public int inc() {
return m + 1;
}
}

constant pool:
#1 = methodref #20.#50 // zero/desk/constantpool/c."<init>":()v
#2 = string #51 // test string
#3 = fieldref #19.#52 // zero/desk/constantpool/constantpool.str:ljava/lang/string;
#4 = fieldref #19.#53 // zero/desk/constantpool/constantpool.a:i
#5 = long 10l
#7 = fieldref #19.#54 // zero/desk/constantpool/constantpool.b:j
#8 = long 100l
#10 = fieldref #19.#55 // zero/desk/constantpool/constantpool.bb:j
#11 = fieldref #19.#56 // zero/desk/constantpool/constantpool.c:i
#12 = float 12.0f
#13 = fieldref #19.#57 // zero/desk/constantpool/constantpool.d:f
#14 = fieldref #19.#58 // zero/desk/constantpool/constantpool.e:f
#15 = double 12.0d
#17 = fieldref #19.#59 // zero/desk/constantpool/constantpool.ee:d
#18 = fieldref #19.#60 // zero/desk/constantpool/constantpool.m:i
#19 = class #61 // zero/desk/constantpool/constantpool
#20 = class #62 // zero/desk/constantpool/c
#21 = class #63 // zero/desk/constantpool/a
#22 = class #64 // zero/desk/constantpool/b
#23 = utf8 str
#24 = utf8 ljava/lang/string;
#25 = utf8 a
#26 = utf8 i
#27 = utf8 constantvalue
#28 = integer 10
#29 = utf8 b
#30 = utf8 j
#31 = utf8 bb
#32 = utf8 c
#33 = utf8 d
#34 = utf8 f
#35 = utf8 e
#36 = utf8 ee
#37 = utf8 d
#38 = utf8 m
#39 = utf8 <init>
#40 = utf8 ()v
#41 = utf8 code
#42 = utf8 linenumbertable
#43 = utf8 localvariabletable
#44 = utf8 this
#45 = utf8 lzero/desk/constantpool/constantpool;
#46 = utf8 inc
#47 = utf8 ()i
#48 = utf8 sourcefile
#49 = utf8 constantpool.java
#50 = nameandtype #39:#40 // "<init>":()v
#51 = utf8 test string
#52 = nameandtype #23:#24 // str:ljava/lang/string;
#53 = nameandtype #25:#26 // a:i
#54 = nameandtype #29:#30 // b:j
#55 = nameandtype #31:#30 // bb:j
#56 = nameandtype #32:#26 // c:i
#57 = nameandtype #33:#34 // d:f
#58 = nameandtype #35:#34 // e:f
#59 = nameandtype #36:#37 // ee:d
#60 = nameandtype #38:#26 // m:i
#61 = utf8 zero/desk/constantpool/constantpool
#62 = utf8 zero/desk/constantpool/c
#63 = utf8 zero/desk/constantpool/a
#64 = utf8 zero/desk/constantpool/b