Learn Java
import com.learnjava.operators.OpInstanceof;

The instanceof Operator in Java

@Concept

instanceof checks whether an object is an instance of a given class or interface at runtime, evaluating to a boolean. It's the standard way to answer "what kind of object is this, really?" before doing something that only makes sense for that specific type — most often right before a cast, to make sure the cast will actually succeed instead of throwing a ClassCastException.

Since Java 16, pattern matching for instanceof lets the check and the cast happen in one expression. if (obj instanceof String s) both confirms obj is a String and immediately declares a new variable s already cast to that type, available for use inside the if block — removing the separate cast statement that used to be required afterward.

@HowItWorks
1

The basic check

obj instanceof SomeClass evaluates to true if obj currently refers to an object that is a SomeClass, or a subclass of it.

2

null is never an instance of anything

obj instanceof SomeClass is simply false whenever obj is null, so it also doubles as a safe null check.

3

Guarding a cast

Checking instanceof before casting avoids a runtime ClassCastException from a cast that wouldn't actually succeed.

4

Pattern matching for instanceof (Java 16+)

if (obj instanceof String s) checks and casts in one step, making s available as an already-typed String inside that block.

5

Works with interfaces too

instanceof isn't limited to classes — checking against an interface confirms the object implements it, however it's actually built underneath.

@Example
public class InstanceofDemo {
public static void main(String[] args) {
Object value = "hello";
if (value instanceof String s) {
System.out.println("Length: " + s.length());
}
System.out.println("Is Integer: " + (value instanceof Integer));
}
}
Output
Length: 5
Is Integer: false
1value instanceof String s confirms value really is a String and, in the same step, binds it to a new variable s already typed as String.
2Inside the if block, s.length() can be called directly, with no separate cast statement needed first.
3value instanceof Integer checks the same object against a different type and correctly evaluates to false, since it's a String, not an Integer.
4Two println calls each run once — one from inside the if block, one after it — giving two lines total.
@UseCases
Safely casting an Object retrieved from a general-purpose collection back to its real type
Branching logic based on which subclass an object actually is at runtime
Avoiding a ClassCastException by checking before casting instead of catching after
Writing a null-safe type check in a single instanceof expression

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.