如果你引用一些文章给你带来麻烦,这将是有帮助的.既然你没有,我将从
ldc的文档中猜出你可能引用的内容:
Otherwise,if the run-time constant pool entry is a symbolic reference
to a class (§5.1),then the named class is resolved (§5.4.3.1) and a
reference to the Class object representing that class,value,is
pushed onto the operand stack.
Otherwise,the run-time constant pool entry must be a symbolic
reference to a method type or a method handle (§5.1). …
该引用有一个链接到JVM规范(5.1)的另一部分,它描述了运行时常量池:
a run-time data structure that serves many of the purposes of the
symbol table of a conventional programming language implementation
这意味着运行时常量池包含有关符号形式的类的部分的信息:作为文本值.
所以,当ldc给一个类的“符号引用”时,它被赋予常量池中CONSTANT_Class_info结构的索引.如果您查看此结构的定义,您将看到它包含对该类名称的引用,也保存在常量池中.
TL; DR:“符号引用”是可用于检索实际对象的字符串.
一个例子:
if (obj.getClass() == String.class) {
// do something
}
成为以下字节码:
aload_1
invokevirtual #21; //Method java/lang/Object.getClass:()Ljava/lang/Class;
ldc #25; //class java/lang/String
if_acmpne 20
在这种情况下,ldc操作是指以符号方式存储的类.当JVM执行此操作码时,它将使用符号引用来标识当前类加载器中的实际类,并返回对类实例的引用.