JVM Spec: A reference to an interface that is not a reference to an object?

Both bytecode operationscheckcast andinstanceof take an objectref on the operand stack to check it against some time refered to via the constant pool. While their principal purpose and function are clear, there is, however, a somewhat puzzling section in the spec:

The following rules are used to determine whether an objectref that is not null can be cast to the resolved type: if S is the class of the object referred to by objectref and T is the resolved class, array, or interface type, checkcast determines whether objectref can be cast to type T as follows:

- If S is an interface type, then:

* If T is a class type, then T must be Object (?.4.7).

* If T is an interface type, then T must be the same interface as S or a superinterface of S (?.13.2).

http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc2.html#checkcast

http://java.sun.com/docs/books/vmspec/2nd-edition/html/Instructions2.doc6.html#instanceof

How can thisever happen: to have an objectref on the stack that is only an interface? Of cource, it could be that the knowledge about an implemented interface is the most that can be determinedat compile time. Nonetheless, the reference will always point to some implementing object at run time. Taking the compile time knowledge as base does not explain this part of the specification as this would effectively mean that a variable of interface type could not be cast to any sensible class except Object. This would not be very practical and is, in fact, not so - at least when taking Sun's J2SE as reference platform.

So, can this part of the spec be read in any sensible way or is it simply nonsense?

[1741 byte] By [thpreussera] at [2007-10-3 2:29:23]
# 1

>

> How can this ever happen: to have an objectref

> on the stack that is only an interface?

As a guess during the linking process. The docs note that those particular ops can produce linking exceptions.

That process wouldn't have anything to do with what was actually on the stack.

jschella at 2007-7-14 19:28:24 > top of Java-index,Java HotSpot Virtual Machine,Specifications...