java中符号引用,Java中的符号引用

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:

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). ...

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.

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: "symbolic references" are strings that can be used to retrieve the actual object.

An example:

if (obj.getClass() == String.class) {

// do something

}

Becomes the following bytecode:

aload_1

invokevirtual #21; //Method java/lang/Object.getClass:()Ljava/lang/Class;

ldc #25; //class java/lang/String

if_acmpne 20

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值