Skip to main content
The instanceof operator is a binary relational operator used to test whether the runtime type of an object reference is assignment-compatible with a specific class, subclass, or interface. It evaluates the type relationship dynamically at runtime and returns a boolean value.

Syntax

Evaluation Rules

The operator evaluates the relationship based on strict type-checking rules:
  1. Subtyping and Inheritance: It returns true if the actual object referred to by objectReference is an instance of Type, or an instance of any direct or indirect subclass/subinterface of Type. Equivalently, for any non-null reference, it returns true if (Type) objectReference would succeed without throwing a ClassCastException.
  2. Null References: If objectReference evaluates to null, the instanceof operator strictly returns false. It never throws a NullPointerException.
  3. Compile-Time Constraints: The Java compiler enforces static type checking on the operator. If the declared type of objectReference and the target Type exist in disjoint inheritance hierarchies (meaning neither can possibly be a subtype of the other), the compiler rejects the expression and throws an inconvertible types error.

Generics and Type Erasure

Due to type erasure, the Java runtime does not retain generic type parameters. Consequently, the instanceof operator cannot be used with parameterized types at runtime, as the JVM cannot verify the generic type argument. Attempting to do so results in a compile-time error. Instead, instanceof must be used with raw types or unbounded wildcards.

Mechanical Example

Pattern Matching for instanceof (Java 16+)

Starting in Java 16, the operator was enhanced with pattern matching, which fuses the type test, the type cast, and variable declaration into a single expression.

Syntax

Mechanics of Pattern Matching

If the instanceof evaluation returns true, the runtime automatically casts the objectReference to the target Type and binds it to the patternVariable. The patternVariable is subject to flow scoping. Its scope is strictly limited to the execution paths where the instanceof condition is guaranteed to be true.
Flow scoping also applies to compound boolean expressions, provided the short-circuiting logic guarantees the type test succeeded before evaluating the right-hand operand:
Tired of Poor Java Skills? Fix That With Deep Grasping!Learn More