Cast object reference


Source code

1class CastObjectReference {
2
3 void m() {
4 Integer integer = new Integer(0);
5 Object object = integer;
6 Integer otherReference = (Integer) object;
7 }
8
9}

Bytecode


Comment

Cast expression is transformed to execution of check that the reference belongs to specified type. Check is performed using checkcast instruction. Instruction checkcast may throw runtime exception ClassCastException if check had failed.

Java compiler version: 21
Other examples
Main page