一 代码
/**
* javap命令查看字节码
*
* @author Hang W
*/
public class ByteCode {
private static int a = 1;
private static int b = 2;
private static int c = 3;
public static int plus() {
return a + b + c;
}
public static void main(String[] args) {
int plus = plus();
System.out.println(plus);
}
}
二 javap 命令
1.首先将java文件编译为class文件
javac ByteCode.java
2.再使用命令,将字节码文件输出到文本中
javap -v ByteCode.class >> ByteCode.txt
三 字节码文件分析
Classfile /C:/Users/hasee/Desktop/springBoot/ModifyFileName/src/com/hang/lab/ByteCode.class
Last modified 2020-2-25; size 620 bytes
MD5 checksum 31c698ca5c9df78ae6d63bfc33391fbd
Compiled from "ByteCode.java"
public class com.hang.lab.ByteCode
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #9.#25 // java/lang/Object."<init>":()V
#2 = Fieldref #8.#26 // com/hang/lab/ByteCode.a:I
#3 = Fieldref #8.#27 // com/hang/lab/ByteCode.b:I
#4 = Fieldref #8.#28 // com/hang/lab/ByteCode.c:I
#5 = Methodref #8.#29 // com/hang/lab/ByteCode.plus:()I
#6 = Fieldref #30.#31 // java/lang/System.out:Ljava/io/PrintStream;
#7 = Methodref #32.#33 // java/io/PrintStream.println:(I)V
#8 = Class #34 // com/hang/lab/ByteCode
#9 = Class #35 // java/lang/Object
#10 = Utf8 a
#11 = Utf8 I
#12 = Utf8 b
#13 = Utf8 c
#14 = Utf8 <init>
#15 = Utf8 ()V
#16 = Utf8 Code
#17 = Utf8 LineNumberTable
#18 = Utf8 plus
#19 = Utf8 ()I
#20 = Utf8 main
#21 = Utf8 ([Ljava/lang/String;)V
#22 = Utf8 <clinit>
#23 = Utf8 SourceFile
#24 = Utf8 ByteCode.java
#25 = NameAndType #14:#15 // "<init>":()V
#26 = NameAndType #10:#11 // a:I
#27 = NameAndType #12:#11 // b:I
#28 = NameAndType #13:#11 // c:I
#29 = NameAndType #18:#19 // plus:()I
#30 = Class #36 // java/lang/System
#31 = NameAndType #37:#38 // out:Ljava/io/PrintStream;
#32 = Class #39 // java/io/PrintStream
#33 = NameAndType #40:#41 // println:(I)V
#34 = Utf8 com/hang/lab/ByteCode
#35 = Utf8 java/lang/Object
#36 = Utf8 java/lang/System
#37 = Utf8 out
#38 = Utf8 Ljava/io/PrintStream;
#39 = Utf8 java/io/PrintStream
#40 = Utf8 println
#41 = Utf8 (I)V
{
public com.hang.lab.ByteCode();
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 8: 0
public static int plus();
descriptor: ()I
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=0, args_size=0
0: getstatic #2 // Field a:I
3: getstatic #3 // Field b:I
6: iadd
7: getstatic #4 // Field c:I
10: iadd
11: ireturn
LineNumberTable:
line 17: 0
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=2, locals=2, args_size=1
0: invokestatic #5 // Method plus:()I
3: istore_1
4: getstatic #6 // Field java/lang/System.out:Ljava/io/PrintStream;
7: iload_1
8: invokevirtual #7 // Method java/io/PrintStream.println:(I)V
11: return
LineNumberTable:
line 21: 0
line 22: 4
line 23: 11
static {};
descriptor: ()V
flags: ACC_STATIC
Code:
stack=1, locals=0, args_size=0
0: iconst_1
1: putstatic #2 // Field a:I
4: iconst_2
5: putstatic #3 // Field b:I
8: iconst_3
9: putstatic #4 // Field c:I
12: return
LineNumberTable:
line 10: 0
line 12: 4
line 14: 8
}
SourceFile: "ByteCode.java"
分析:包含运行时常量池、局部变量表大小、操作数栈深度、动态连接(符号引用)、调用静态方法等。