In these days I have been playing with Java reflection and .class
format. I'm currently studying ldc
instruction.
In JVM Specification I found term I don't understand: symbolic reference, and I have the following questions.
What does it mean?
Where is it used?
- In which cases does the
ldc
instruction load a symbolic reference? - Is there any code in Java corresponding to that action?
It would be helpful if you would quote the exact piece of the documentation that's giving you trouble. Since you haven't, I'm going to take a guess at what you might have quoted, from the doc for ldc:
This quote has a link to another section of the JVM spec (5.1), which describes the run-time constant pool:
What this means is that the run-time constant pool contains information about the pieces of a class in symbolic form: as text values.
So, when
ldc
is given a "symbolic reference" to a class, it's given the index of aCONSTANT_Class_info
structure within the constant pool. If you look at the definition of this structure, you'll see that it contains a reference to the name of the class, also held within the constant pool.TL;DR: "symbolic references" are strings that can be used to retrieve the actual object.
An example:
Becomes the following bytecode:
In this case, the
ldc
operation refers to a class that is stored symbolically. When the JVM executes this opcode, it will use the symbolic reference to identify the actual class within the current classloader, and return a reference to the class instance.