Java中的符号引用

前端之家收集整理的这篇文章主要介绍了Java中的符号引用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在这几天我一直在玩 Java反射和.class格式.我正在学习ldc指令.

在JVM规范中,我发现术语我不明白:符号引用,我有以下问题.

这是什么意思?
>它在哪里使用?
>在哪种情况下ldc指令加载符号引用?
> Java中是否有与该动作相对应的代码

解决方法

如果你引用一些文章给你带来麻烦,这将是有帮助的.既然你没有,我将从 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执行此操作码时,它将使用符号引用来标识当前类加载器中的实际类,并返回对类实例的引用.

原文链接:https://www.f2er.com/java/122474.html

猜你在找的Java相关文章