Symbolic references:
Reference symbol (Symbolic References): Symbol reference to a certain set of symbols described in the referenced, literal symbols may be in any form, can be unambiguous target as long as the target can be used. For example, it appears to CONSTANT_Class_info, CONSTANT_Fieldref_info, CONSTANT_Methodref_info other types of constants in the Class file. Symbol reference has nothing to do with the memory layout of the virtual machine, the goal is not necessarily referenced loaded into memory. In Java, a class will be compiled into a java class file. At compile time, java class does not know the actual address of the referenced class, you can only use symbolic references instead. For example org.simple.People class references org.simple.Language class, the class at compile time People do not know the actual memory address Language class, it can only use the symbol org.simple.Language (assuming that this is, of course, practice is ) is represented by the constant address Language classes similar to CONSTANT_Class_info be represented. Various virtual machine implementation of the memory location may vary, but they can accept symbolic references are the same, because the literal form of symbolic references clearly defined in the Java Virtual Machine specification Class file format.
Direct quote:
May be a direct reference
(1) direct pointer to the object (for example, point "Type" [] Class object, class variables, class methods may direct reference is a pointer to the method area)
(2) relative offset (for example, point instance variables, direct reference to the offset are examples of methods)
(3) a handle to indirectly locate the target
directly reference and is related to layout of the virtual machine, the same reference symbols translated in a different virtual machine instances direct references are generally not the same. If you have a direct reference to that target reference must have been loaded into the memory.
I understand the meaning of symbolic references achievement is a description of some of the approximately jvm reference target, using some notation. So being the first to understand
Here are some ways my analysis
javap command is a disassembler tool comes with the jdk, you can decompile the class files compiled by the command, view some java some internal instruction execution.
javap -c -l -p -v A.class I generally use
Directly on the code
package com.jvm.classloading;
public class B {
public static void main(String[] args) {
}
public int b1(int a,int b){
int c = b2(a,b);
return c;
}
public int b2(int a,int b){
return a+b;
}
}
Resolves to use javap
Classfile /C:/Users/able/IdeaProjects/jvm/src/com/jvm/classloading/B.class
Last modified 2020-4-4; size 389 bytes
MD5 checksum 7ea5d05de8809bd5775b1195eb296819
Compiled from "B.java"
public class com.jvm.classloading.B
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Methodref #4.#16 // java/lang/Object."<init>":()V
#2 = Methodref #3.#17 // com/jvm/classloading/B.b2:(II)I
#3 = Class #18 // com/jvm/classloading/B
#4 = Class #19 // java/lang/Object
#5 = Utf8 <init>
#6 = Utf8 ()V
#7 = Utf8 Code
#8 = Utf8 LineNumberTable
#9 = Utf8 main
#10 = Utf8 ([Ljava/lang/String;)V
#11 = Utf8 b1
#12 = Utf8 (II)I
#13 = Utf8 b2
#14 = Utf8 SourceFile
#15 = Utf8 B.java
#16 = NameAndType #5:#6 // "<init>":()V
#17 = NameAndType #13:#12 // b2:(II)I
#18 = Utf8 com/jvm/classloading/B
#19 = Utf8 java/lang/Object
{
public com.jvm.classloading.B();
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 3: 0
public static void main(java.lang.String[]);
descriptor: ([Ljava/lang/String;)V
flags: ACC_PUBLIC, ACC_STATIC
Code:
stack=0, locals=1, args_size=1
0: return
LineNumberTable:
line 7: 0
public int b1(int, int);
descriptor: (II)I
flags: ACC_PUBLIC
Code:
stack=3, locals=4, args_size=3
0: aload_0
1: iload_1
2: iload_2
3: invokevirtual #2 // Method b2:(II)I
6: istore_3
7: iload_3
8: ireturn
LineNumberTable:
line 10: 0
line 11: 7
public int b2(int, int);
descriptor: (II)I
flags: ACC_PUBLIC
Code:
stack=2, locals=3, args_size=3
0: iload_1
1: iload_2
2: iadd
3: ireturn
LineNumberTable:
line 15: 0
}
SourceFile: "B.java"
First we look at this method b1, b1 method called b2, b1 is to look at how to call b2, how resolved
1 aload_0, 2 iload_1, 3 iload_2 jvm three bytes out explanation, the parameter 1 and parameter 2 in the stack.
The main facie
3: invokevirtual #2 // Method b2:(II)I
invokevirtual mean real column method call, this call that it? Note # 2 has a look back, we find analytic javap found as follows
#2 = Methodref #3.#17 // com/jvm/classloading/B.b2:(II)I
I found # 2 # 3 # = 17 careful not to look away. We also took a look at # 3 and # 17
#3 = Class #18 // com/jvm/classloading/B
#17 = NameAndType #13:#12 // b2:(II)I
There's found the back with a #, really, look at # 18
#18 = Utf8 com/jvm/classloading/B
Finally found, # 18 represents the class I, class of Road King
Looking at # 17
#12 = Utf8 (II)I
#13 = Utf8 b2
Assembly according to # 17 obtained in b2: (II) I bound out according to the code, there are two parameters parentheses, I is parentheses return parameters
in accordance with the assembled # 2 com / jvm / classloadfing / B.b2 (II ) I;
# 2 Where do you see the comment, has put here to parse out, we just follow the order, find out the sentence.
From here you can see symbolic references with call b2 method # 2 is represented here can be understood as # 2 is symbolic references.
I now have a question, why use symbolic references, direct direct reference to use, but also not so complicated.
Follow-up to be improved.