首先写一个Java文件
public class HelloWorld{
private int m;
public int intc(){
return m + 1;
}
}
使用javac编译为class文件
C:UsersxiaoqiuDesktop>javac HelloWorld.java
使用十六进制编辑器winHex打开class文件
上面我们人工读class文件实在是一个比较繁琐并且吃力的活,下面我们采用javap -verbose 来读取
C:UsersxiaoqiuDesktop>javap -verbose HelloWorld.class
Classfile /C:/Users/xiaoqiu/Desktop/HelloWorld.class
Last modified 2019-1-26; size 278 bytes
MD5 checksum 0a90643d0d7ef2078965014eca999f28
Compiled from "HelloWorld.java"
public class HelloWorld
minor version: 0
major version: 52#主版本号
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:#常量池 可以看出和我们读的结果是一样的
#1 = Methodref #4.#15 // java/lang/Object."<init>":()V
#2 = Fieldref #3.#16 // HelloWorld.m:I
#3 = Class #17 // HelloWorld
#4 = Class #18 // java/lang/Object
#5 = Utf8 m
#6 = Utf8 I
#7 = Utf8 <init>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 intc
#12 = Utf8 ()I
#13 = Utf8 SourceFile
#14 = Utf8 HelloWorld.java
#15 = NameAndType #7:#8 // "<init>":()V
#16 = NameAndType #5:#6 // m:I
#17 = Utf8 HelloWorld
#18 = Utf8 java/lang/Object
{
public HelloWorld();
descriptor: ()V
flags: ACC_PUBLIC
Code:
stack=1, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
LineNumberTable:
line 1: 0
public int intc();
descriptor: ()I
flags: ACC_PUBLIC
Code:
stack=2, locals=1, args_size=1
0: aload_0
1: getfield #2 // Field m:I
4: iconst_1
5: iadd
6: ireturn
LineNumberTable:
line 4: 0
}
SourceFile: "HelloWorld.java"
下面附上常量池的项目类型
以及每种常量类型的结构