关系表达式实例中的 ReferenceType 是什么类型的对象?

What type of object is the ReferenceType in an instanceof RelationalExpression?

实际上,Java 需要一个对象作为其大部分语义的输入和输出,无论是原始类型还是实际对象,不包括类型名称、参数列表等 class 元数据, 和 return 类型

本质上是一种 OOP 语言,在抽象层面上,一切都被视为对象,无论一切是否都作为对象存储在内存中

所以,我想知道 ReferenceType in the production, RelationalExpression (instanceof) 是什么类型的对象?

例如,在下面的表达式中:

object instanceof ReferenceType

我想知道 ReferenceType 是否是一个实际的对象,如果不是,根据 JVM 规范,它是如何被 JVM 表示为字节码的,它是如何工作的?

In practice, Java expects an object for an input and output for everything,

嗯,不,这是言过其实了。即使我们排除标点符号和关键字等语法元素,Java 也具有原始类型的值。它有类型名称。

and, being inherently an OOP language, everything is treated as an object,

你因为随便乱说 "everything" 这个词而给自己惹上了麻烦。我认为您试图断言在 OO 语言中所有 data 都在程序内部由对象表示。至少那是我能达到的最接近似是而非的解释,事实上某些 OO 语言(SmallTalk、Ruby、...)也是如此——但不是 Java.

so I was wondering, what sort of object is the ReferenceType in the production, RelationalExpression (instanceof)?

这不是一个对象。这是一个类型名称。

How is it represented in the JVM at runtime?

我必须检查才能确定,但​​很可能它是由对出现表达式的 class 的常量池的引用表示的。

What sort of objects can be passed to this Expression, if any, if usually a type is used in Declarations such as class declarators or variable declarators?

instanceof 表达式的右手操作数是一个类型名称,就像在 class 和变量声明符中使用的一样。它不是一个值,既不是引用也不是原始类型。

Is it a "meta-object" that only represents a real type or class,

Java语言没有"meta object"的概念,至少与java.lang.Class类型的对象区别开来。但是 Class 类型的对象仍然是对象,对它们的引用不适合用作 instanceof 表达式的右手操作数。右侧操作数必须是类型名称。

and why would the developers of Java not allow passing a Class object in place of a ReferenceType, or am I thinking in the wrong paradigm for instanceof?

我不能权威地谈论 Java 设计师的动机,我不是他们中的一员。然而,我确实观察到 Class.isInstance() 自 Java 1.1 起可用,其目的与允许引用 Class 作为 instanceof 的右侧操作数相同表达式就可以了。

instanceof 的右手操作数必须是类型名称。

ReferenceType 是符号引用。根据 Java 虚拟机规范 (Java 10),instanceof 有对应的 instanceof indexbyte1 indexbyte2 指令。这两个索引字节可以构造成一个 32 位(无符号)整数,它是当前 class 的运行时常量池的索引。

The objectref, which must be of type reference, is popped from the operand stack. The unsigned indexbyte1 and indexbyte2 are used to construct an index into the run-time constant pool of the current class (§2.6), where the value of the index is (indexbyte1 << 8) | indexbyte2. The run-time constant pool item at the index must be a symbolic reference to a class, array, or interface type.

其中 objectref 是 instanceof 表达式中左侧的对象引用。

如果 objectref 不为 null,则解析符号引用,并根据已解析的类型评估 objectref 的类型以确定 objectref 是否是 ReferenceType 的实例:

If objectref is null, the instanceof instruction pushes an int result of 0 as an int onto the operand stack.

Otherwise, the named class, array, or interface type is resolved (§5.4.3.1). If objectref is an instance of the resolved class or array type, or implements the resolved interface, the instanceof instruction pushes an int result of 1 as an int onto the operand stack; otherwise, it pushes an int result of 0.