Java中的符号引用

编程入门 行业动态 更新时间:2024-10-23 21:36:48
本文介绍了Java中的符号引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在这些日子里,我一直在玩Java反射和 .class 格式。我目前正在学习 ldc 指令。

In these days I have been playing with Java reflection and .class format. I'm currently studying ldc instruction.

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

In JVM Specification I found term I don't understand: symbolic reference, and I have the following questions.

  • 这是什么意思?

  • What does it mean?

    它在哪里使用?

    推荐答案

    如果引用它会很有帮助给你带来麻烦的文件的确切部分。既然你还没有,我将从 ldc :

    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:

    否则,如果是运行时常量池entry是一个符号引用到类(第5.1节),然后命名类被解析(第5.4.3.1节)和引用Class对象表示该类,值是推入操作数堆栈。

    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.

    否则,运行时常量池条目必须是对方法类型或方法句柄的符号引用(§ 5.1)。 ...

    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)的另一部分的链接,该部分描述了运行时常量池:

    This quote has a link to another section of the JVM spec (5.1), which describes the run-time constant pool:

    运行时数据结构,用于传统编程语言实现的符号表的许多用途

    a run-time data structure that serves many of the purposes of the symbol table of a conventional programming language implementation

    这意味着运行时常量池包含有关符号形式的类的各个部分的信息:作为文本值。

    What this means is that the run-time constant pool contains information about the pieces of a class in symbolic form: as text values.

    因此,当 ldc 给出一个类的符号引用时,它的索引为 CONSTANT_Class_info 常量池中的结构。如果你看一下这个结构的定义,你会看到它包含对类名的引用,也是在常量池中保存的。

    So, when ldc is given a "symbolic reference" to a class, it's given the index of a CONSTANT_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:符号引用是可用于检索实际对象的字符串。

    TL;DR: "symbolic references" are strings that can be used to retrieve the actual object.

    一个例子:

    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 operation是指以符号形式存储的类。当JVM执行此操作码时,它将使用符号引用来标识当前类加载器中的实际类,并返回对类实例的引用。

    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.

  • 更多推荐

    Java中的符号引用

    本文发布于:2023-11-12 20:36:54,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1582472.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:符号   Java

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!