---
spec: JVMS
title: "The Java Virtual Machine Specification"
edition: "Java SE 9 Edition"
java_version: 9
chapter: "Chapter 6. The Java Virtual Machine Instruction Set"
section: "6.5.checkcast"
heading: "checkcast"
parent: "6.5. Instructions"
source: https://docs.oracle.com/javase/specs/jvms/se9/html/jvms-6.html#jvms-6.5.checkcast
id: "6.5.checkcast"
url: https://jvm.obfuscated.software/jvms/se9/6.5.checkcast.html
raw: https://jvm.obfuscated.software/jvms/se9/6.5.checkcast.md
---

# checkcast

## Operation

Check whether object is of given type

## Format

```
checkcast
indexbyte1
indexbyte2
```

## Forms

*checkcast* = 192 (0xc0)

## Operand Stack

..., *objectref* →

..., *objectref*

## Description

The *objectref* must be of type `reference`. The unsigned *indexbyte1* and *indexbyte2* are used to construct an index into the run-time constant pool of the current class ([§2.6](2.6.md)), 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.

If *objectref* is `null`, then the operand stack is unchanged.

Otherwise, the named class, array, or interface type is resolved ([§5.4.3.1](5.4.3.1.md)). If *objectref* can be cast to the resolved class, array, or interface type, the operand stack is unchanged; otherwise, the *checkcast* instruction throws a `ClassCastException`.

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 type of the object referred to by *objectref*, and T is the resolved class, array, or interface type, then *checkcast* determines whether *objectref* can be cast to type T as follows:

- If S is a class type, then:

  - If T is a class type, then S must be the same class as T, or S must be a subclass of T;
  - If T is an interface type, then S must implement interface T.

- If S is an array type SC`[]`, that is, an array of components of type SC, then:

  - If T is a class type, then T must be `Object`.

  - If T is an interface type, then T must be one of the interfaces implemented by arrays (JLS §4.10.3).

  - If T is an array type TC`[]`, that is, an array of components of type TC, then one of the following must be true:

    - TC and SC are the same primitive type.
    - TC and SC are reference types, and type SC can be cast to TC by recursive application of these rules.

## Linking Exceptions

During resolution of the symbolic reference to the class, array, or interface type, any of the exceptions documented in [§5.4.3.1](5.4.3.1.md) can be thrown.

## Run-time Exception

Otherwise, if *objectref* cannot be cast to the resolved class, array, or interface type, the *checkcast* instruction throws a `ClassCastException`.

## Notes

The *checkcast* instruction is very similar to the *instanceof* instruction ([§*instanceof*](6.5.instanceof.md)). It differs in its treatment of `null`, its behavior when its test fails (*checkcast* throws an exception, *instanceof* pushes a result code), and its effect on the operand stack.
