动态链接:
动态链接中存放所有方法的JVM指令码的内存地址。
public class Math {
public static final Integer CONSTANT=444;
public int compute(){ //一个方法会创建一块栈帧内存区域
int a=1;
int b=2;
int c=(a+b)*10;
return c;
}
public static void main(String[] args) {
Math math=new Math();
math.compute();
Math math2=new Math();
math2.compute();
System.out.println("test");
}
}
使用javap 命令查看jvm指令
javap -v Math.Class > Math.txt
生成的Math.text
Classfile /E:/idea_java/jvm/target/classes/com/iotek/jvm/Math.class
Last modified 2020-6-11; size 757 bytes
MD5 checksum 0b2183b64c2ba6447976a55e7d94bc3b
Compiled from "Math.java"
public class com.iotek.jvm.Math
minor version: 0
major version: 49
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #7.#31 // java/lang/Object."<init>":()V
#2 = Class #32 // com/iotek/jvm/Math
#3 = Methodref #2.#31 // com/iotek/jvm/Math."<init>":()V
#4 = Methodref #2.#33 // com/iotek/jvm/Math.compute:()I
#5 = Methodref #34.#35 // java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
#6 = Fieldref #2.#36 // com/iotek/jvm/Math.CONSTANT:Ljava/lang/Integer;
#7 = Class #37 // java/lang/Object
#8 = Utf8 CONSTANT
#9 = Utf8 Ljava/lang/Integer;
#10 = Utf8 <init>
#11 = Utf8 ()V
#12 = Utf8 Code
#13 = Utf8 LineNumberTable
#14 = Utf8 LocalVariableTable
#15 = Utf8 this
#16 = Utf8 Lcom/iotek/jvm/Math;
#17 = Utf8 compute
#18 = Utf8 ()I
#19 = Utf8 a
#20 = Utf8 I
#21 = Utf8 b
#22 = Utf8 c
#23 = Utf8 main
#24 = Utf8 ([Ljava/lang/String;)V
#25 = Utf8 args
#26 = Utf8 [Ljava/lang/String;
#27 = Utf8 math
#28 = Utf8 <clinit>
#29 = Utf8 SourceFile
#30 = Utf8 Math.java
#31 = NameAndType #10:#11 // "<init>":()V
#32 = Utf8 com/iotek/jvm/Math
#33 = NameAndType #17:#18 // compute:()I
#34 = Class #38 // java/lang/Integer
#35 = NameAndType #39:#40 // valueOf:(I)Ljava/lang/Integer;
#36 = NameAndType #8:#9 // CONSTANT:Ljava/lang/Integer;
#37 = Utf8 java/lang/Object
#38 = Utf8 java/lang/Integer
#39 = Utf8 valueOf
#40 = Utf8 (I)Ljava/lang/Integer;
{
public static final java.lang.Integer CONSTANT;
descriptor: Ljava/lang/Integer;
flags: ACC_PUBLIC, ACC_STATIC, ACC_FINAL
public com.iotek.jvm.Math();
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 11: 0
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 this Lcom/iotek/jvm/Math;
public int compute();
descriptor: ()I
flags: ACC_PUBLIC
Code:
stack=2, locals=4, args_size=1
0: iconst_1
1: istore_1
2: iconst_2
3: istore_2
4: iload_1
5: iload_2
6: iadd
7: bipush 10
9: imul
10: istore_3
11: iload_3
12: ireturn
LineNumberTable:
line 15: 0
line 16: 2
line 17: 4
line 18: 11
LocalVariableTable:
Start Length Slot Name Signature
0 13 0 this Lcom/iotek/jvm/Math;
2 11 1 a I
4 9 2 b I
11 2 3 c I
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: new #2 // class com/iotek/jvm/Math
3: dup
4: invokespecial #3 // Method "<init>":()V
7: astore_1
8: aload_1
9: invokevirtual #4 // Method compute:()I
12: pop
13: return
LineNumberTable:
line 22: 0
line 23: 8
line 24: 13
LocalVariableTable:
Start Length Slot Name Signature
0 14 0 args [Ljava/lang/String;
8 6 1 math Lcom/iotek/jvm/Math;
static {};
descriptor: ()V
flags: ACC_STATIC
Code:
stack=1, locals=0, args_size=0
0: sipush 444
3: invokestatic #5 // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
6: putstatic #6 // Field CONSTANT:Ljava/lang/Integer;
9: return
LineNumberTable:
line 12: 0
}
SourceFile: "Math.java"
Math.Class会被类装载子系统加载到方法区,程序运行时会更据堆中的math对象的头指针找到指令码的内存地址,然后将指令码的内存地址放在动态链接区域中。之后在执行compute()方法时会更据动态链接找到指令码的内存地址