了解常量池的javap输出 [英] Understanding javap's output for the Constant Pool
问题描述
在一个非常简单的HelloWorld应用程序上运行javap时,我对常量池周围的输出有一些混淆。
When running javap on a very simple HelloWorld application I have some confusion on the output around the constant pool.
测试代码
public class TestClass {
public static void main(String[] args) {
System.out.println("hello world");
}
}
Javap -c -verbose输出(剪断) )
// Header + consts 1..22 snipped
const #22 = String #23; // hello world
const #23 = Asciz hello world;
public static void main(java.lang.String[]);
Signature: ([Ljava/lang/String;)V
Code:
Stack=2, Locals=1, Args_size=1
0: getstatic #16; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #22; //String hello world
5: invokevirtual #24; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
// Debug info snipped
}
好的,所以在第3行我们看到通过#22将hello world常量推送到堆栈上,但const#23似乎保持实际价值。我想我对#(数字)在打印输出右侧出现时的含义有点困惑。
Ok, so on line 3 we see a pushing of the "hello world" constant onto the stack via #22, but const #23 seems to hold the actual value. I guess I am a little confused with what the #(number) means when it appears on the right-hand-side of the printout.
Oracle / Sun的javap的手册页还有很多不足之处。
Oracle/Sun's man page for javap leaves much to be desired.
推荐答案
所有类
,界面
,字段
名称和字符串
常量进入java 常量池。
All your class
, interface
, field
names and string
constants go into the java constant pool.
根据VM规范( http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html :
constant_pool是一个
结构表(§4.4),表示各种
字符串常量,类和接口
名称,字段名称和其他
常量在
中引用ClassFile结构及其
子结构。每个
constant_pool表条目的格式由其第一个标记字节表示为
。
constant_pool表从1
索引到constant_pool_count-1。
The constant_pool is a table of structures (§4.4) representing various string constants, class and interface names, field names, and other constants that are referred to within the ClassFile structure and its substructures. The format of each constant_pool table entry is indicated by its first "tag" byte. The constant_pool table is indexed from 1 to constant_pool_count-1.
所以就常量池而言如下所示可以被视为:
So in terms of constant pool something like below can be viewed as:
const #22 = String #23; // hello world
const #23 = Asciz hello world;
#22(索引22)的值类型为 String
并且其值为空终止c string( Asciz ) hello world
位于指数23。
The value at #22 (index 22) is of type String
and its value is null terminated c string (Asciz) hello world
is at index 23.
这篇关于了解常量池的javap输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!