学习JVM原理-10. 用javap分析bytecode文件中的Constant Pool

受教于《深入理解Java虚拟机》周志明著

直接看ByteCode文件会累死的,我们可以用JDK附带的javap -verbose A.class 来生成可读的信息.

下面就举个例子,看下这类信息中的Constant Pool部分

package pool;

public class PlayConstant {

	private static final int DEFAULT_PLAYER_COUNT = 55;

	private String gameName;

	public Long getMoney(int playerCount) {
		long money = 10 * playerCount;
		return money;
	}

}

一起来分析一下它的Constant Pool:

  

const #1 = class	#2;	//  pool/PlayConstant  --#2意思是它的值在Pool中的第#2个位置
const #2 = Asciz	pool/PlayConstant;
const #3 = class	#4;	//  java/lang/Object   --父类
const #4 = Asciz	java/lang/Object;
const #5 = Asciz	DEFAULT_PLAYER_COUNT;          --常量名
const #6 = Asciz	I;                             --常量类型,I意思是int
const #7 = Asciz	ConstantValue;                 --将为常量设值
const #8 = int	55;                                    --常量值为55
const #9 = Asciz	gameName;                      --成员变量名    
const #10 = Asciz	Ljava/lang/String;;            --成员变量类型,凡是对象都会在类型前加个"L"
const #11 = Asciz	<init>;                        --构造方法
const #12 = Asciz	()V;                           --构造方法为void
const #13 = Asciz	Code;                          
const #14 = Method	#3.#15;	//  java/lang/Object."<init>":()V --所调用的父类构造方法的签名
const #15 = NameAndType	#11:#12;//  "<init>":()V
const #16 = Asciz	LineNumberTable;               --编译时记录了Line Number
const #17 = Asciz	LocalVariableTable;            --编译时记录了变量名
const #18 = Asciz	this;                          --this_class
const #19 = Asciz	Lpool/PlayConstant;;           --本类的全名
const #20 = Asciz	getMoney;                      --成员方法名
const #21 = Asciz	(I)Ljava/lang/Long;;           --成员方法返回类型
const #22 = Method	#23.#25;	//  java/lang/Long.valueOf(J)Ljava/lang/Long;
const #23 = class	#24;	//  java/lang/Long
const #24 = Asciz	java/lang/Long;
const #25 = NameAndType	#26:#27;//  valueOf:(J)Ljava/lang/Long;
const #26 = Asciz	valueOf;
const #27 = Asciz	(J)Ljava/lang/Long;;           --方法的参数的类型
const #28 = Asciz	playerCount;                   --方法的参数名
const #29 = Asciz	money;                         --方法里面的局部变量名
const #30 = Asciz	J;                             --局部变量类型: long
const #31 = Asciz	SourceFile;                    --编译时记录了源文件名
const #32 = Asciz	PlayConstant.java;             --源文件名


Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.