当前位置:网站首页>What are the symbolic and direct references of the JVM

What are the symbolic and direct references of the JVM

2022-06-26 10:12:00 ZZ learn java well

Analysis 1 :
A symbolic reference is a class ( Not just classes, of course , It also includes other parts of the class , Such as method , Field etc. ), Other classes are introduced , But JVM I don't know where the other classes introduced are , So we use the unique symbol instead of , Wait until the classloader resolves , Just find the address of the reference class by referring to the symbol , This address is a direct reference .

Analysis 2 :
1. Symbol reference (Symbolic References):

A symbolic reference describes the referenced target with a set of symbols , A symbol can be any form of literal quantity , As long as it can be used to locate the target without ambiguity . for example , stay Class In the document it uses CONSTANT_Class_info、CONSTANT_Fieldref_info、CONSTANT_Methodref_info Constant of the same type appears . Symbol references are independent of the memory layout of the virtual machine , Referenced targets are not necessarily loaded into memory . stay Java in , One java Class will compile into a class file . At compile time ,java Class does not know the actual address of the referenced class , So we can only use symbolic references instead of . such as org.simple.People Class references org.simple.Language class , At compile time People Class does not know Language Class's actual memory address , So only symbols can be used org.simple.Language( Suppose it's this , Of course, in practice, it is similar to CONSTANT_Class_info Is represented by a constant of ) To express Language Class address . The memory layout of various virtual machine implementations may be different , But the symbolic references they can accept are consistent , Because the literal form of symbolic reference is clearly defined in Java Virtual machine specification Class In file format .

2. Direct reference :
A direct quote can be
(1) Pointer directly to the target ( such as , Point to “ type ”【Class object 】、 Class variables 、 A direct reference to a class method may be a pointer to a method area )
(2) Relative offset ( such as , Point to instance variable 、 Direct references to instance methods are offsets )
(3) A handle that can be indirectly located to the target
Direct references are related to the layout of the virtual machine , The direct references translated from the same symbolic reference on different virtual machine instances will not be the same . If there is a direct quote , The target of the reference must have been loaded into memory .

Analysis three :
Symbolic references are used immediately **( In the form of string symbols )** To denote a reference , In fact, the quoted class 、 Methods or variables have not yet been loaded into memory . A direct reference is a pointer with a specific reference address , The referenced class 、 Methods or variables have been loaded into memory . Take variables for example :

Symbol references need to be converted to direct references to be valid , It also shows that direct reference is more efficient than symbol reference . Then why do we use symbolic references ? This is because before the class loads ,javac Will compile the source code into .class file , This is the time javac I don't know the class referenced in the compiled class 、 Methods or variables where their reference address is , So we can only use symbolic references to express , Of course , The symbolic reference is to follow java Virtual machine specification . There is also a situation where symbols are used to refer to , For example, the example of symbolic reference of variables mentioned above , For clarity of logic and readability of code .
javac Compiling a A Class time , If A Class applied B class , that javac Go to the classpath to find B class , If you find it , Just put A Class to A.class file , At this time A.class There's one in the file B Symbol reference of class ( String form ), You can write two for yourself A Classes and B class ,A Class reference B class , compile A After the class , Use JDK Built in decompiler , Decompile the code to see

We all know , Class loading process is divided into loading —> verification —> Get ready —> analysis —> Initialize this 5 Stages , The conversion of symbolic references to direct references occurs during the parsing phase , The parsing phase may occur before initialization , Or after initialization .
 

原网站

版权声明
本文为[ZZ learn java well]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260922193116.html