符号引用
我们用命令javap -v Math.class 获取可读的字节码信息,如下
我们类信息中的所有符号都放在了常量池中,如下:
...public class com.suibibk.jvm.Mathminor version: 0major version: 52flags: ACC_PUBLIC, ACC_SUPERConstant pool:#1 = Class #2 // com/suibibk/jvm/Math#2 = Utf8 com/suibibk/jvm/Math#3 = Class #4 // java/lang/Object#4 = Utf8 java/lang/Object#5 = Utf8 <init>#6 = Utf8 ()V#7 = Utf8 Code#8 = Methodref #3.#9 // java/lang/Object."<init>":()V#9 = NameAndType #5:#6 // "<init>":()V#10 = Utf8 LineNumberTable#11 = Utf8 LocalVariableTable#12 = Utf8 this#13 = Utf8 Lcom/suibibk/jvm/Math;#14 = Utf8 compute#15 = Utf8 ()I#16 = Utf8 a#17 = Utf8 I#18 = Utf8 b#19 = Utf8 c#20 = Utf8 main#21 = Utf8 ([Ljava/lang/String;)V#22 = Methodref #1.#9 // com/suibibk/jvm/Math."<init>":()V#23 = Methodref #1.#24 // com/suibibk/jvm/Math.compute:()I#24 = NameAndType #14:#15 // compute:()I#25 = Utf8 args#26 = Utf8 [Ljava/lang/String;#27 = Utf8 math#28 = Utf8 SourceFile#29 = Utf8 Math.java{public com.suibibk.jvm.Math();descriptor: ()Vflags: ACC_PUBLICCode:stack=1, locals=1, args_size=10: aload_01: invokespecial #8 // Method java/lang/Object."<init>":()V4: returnLineNumberTable:line 3: 0LocalVariableTable:Start Length Slot Name Signature0 5 0 this Lcom/suibibk/jvm/Math;public int compute();descriptor: ()Iflags: ACC_PUBLICCode:stack=2, locals=4, args_size=10: iconst_11: istore_12: iconst_23: istore_24: iload_15: iload_26: iadd7: bipush 109: imul10: istore_311: iload_312: ireturnLineNumberTable:line 5: 0line 6: 2line 7: 4line 8: 11LocalVariableTable:Start Length Slot Name Signature0 13 0 this Lcom/suibibk/jvm/Math;2 11 1 a I4 9 2 b I11 2 3 c Ipublic static void main(java.lang.String[]);descriptor: ([Ljava/lang/String;)Vflags: ACC_PUBLIC, ACC_STATICCode:stack=2, locals=2, args_size=10: new #1 // class com/suibibk/jvm/Math3: dup4: invokespecial #22 // Method "<init>":()V7: astore_18: aload_19: invokevirtual #23 // Method compute:()I12: pop13: returnLineNumberTable:line 11: 0line 12: 8line 13: 13LocalVariableTable:Start Length Slot Name Signature0 14 0 args [Ljava/lang/String;8 6 1 math Lcom/suibibk/jvm/Math;}
常量池中的就都是符号引用,那方法怎么找到。
符号引用转直接引用
我们再看一下我们的代码
public class Math {private static Math math = new Math();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();}}
在JVM执行到map.compute()时,也就是对于上面的指令
9: invokevirtual #23 // Method compute:()I
时,怎么根据符号引用找到compute()的执行指令?
我们通过#23去常量池中找到对应的符号
#23 = Methodref #1.#24 // com/suibibk/jvm/Math.compute:()I
可以知道时方法引用,其实#23对应也有两个符号#1和#24,这两个符号分别如下
#1 = Class #2 // com/suibibk/jvm/Math
#24 = NameAndType #14:#15 // compute:()I
然后#24对应的时#14和#15
#14 = Utf8 compute
#15 = Utf8 ()I
所以#23对应的符号引用就是com/suibibk/jvm/Math.compute:()I
然后JVM会找到该符号引用对应的直接引用,放入栈帧的动态链接中。
静态链接
在类加载的解析阶段,会将符号引用替换为直接引用,该阶段会把一些
静态方法(符号引用比如main()方法)替换为指向数据所在内存的指针或句柄(直接引用),这就是所谓的
静态链接过程(在类的加载期间完成)
动态链接
动态链接就是指上面在程序运行期间把compute()方法的符号引用替换为直接引用的过程
