Instanceof operator

The instanceof operator checks at runtime whether a heap object is an instance of a class or any of its subtypes.

Warning
The instanceof operator currently only works on GC-allocated types, and not on custom allocator types.

Syntax

Unlike in many other languages with C-like syntax, the precedence of the instanceof operator is greater than that of the logical not "!" operator, which means that to invert the result, no brackets are required. This is the same as for extends.

Example

The following example creates an array of animals. The array can store any subtype of class Animal, and instanceof is used to log a special message to the console when an animal is a weasel. The instanceof operator is used in lines 13 and 14.