instanceof & pattern matching in Java
if (o instanceof String s) binds a typed variable.
instanceof asks "are you a...?"
x instanceof Dog is true if x is a Dog or a subtype of Dog. For null it's always false — it never throws.
Object o = "Java";
boolean a = o instanceof String; // true
boolean b = o instanceof Integer; // falseTest, cast, use
if (o instanceof String) {
String s = (String) o;
use(s.length());
}Repeats the type; easy to cast to the wrong one.
if (o instanceof String s) {
use(s.length());
}Tests and binds s, already cast (Java 16).
Test and use in one breath
What does this print?
Object o = 7;
if (o instanceof Integer n && n > 5) {
System.out.println("big " + (n * 2));
} else {
System.out.println("small");
}big 14big 72small
Show the answer
big 14. o is an Integer, so n is bound. && only evaluates n > 5 when the match succeeded, so n is safe to use there.
Is null an Object?
What does this print?
Object o = null;
System.out.println(o instanceof Object);truefalseThrows NullPointerException
Show the answer
false — even for Object. instanceof checks the object, and null refers to no object at all.
|| breaks the binding
With ||, the right side runs only when the left side was false — when the match failed. So s isn't bound there, and the compiler refuses: it cannot find s. && works; || doesn't.
if (o instanceof String s || s.isEmpty()) {
// compile error: s not in scope
}Flow scoping
The compiler tracks where a match is certain. If a failed match makes the method return, then s is bound for everything after the if. This early-exit style avoids deep nesting.
if (!(o instanceof String s)) {
return;
}
System.out.println(s.length()); // OKIn real projects
You'll write o instanceof Point p && x == p.x in nearly every equals method. And pattern matching grew from here: switch patterns and record patterns (Java 21) build on exactly this idea.
Key takeaways
- instanceof is false for null
- o instanceof String s tests and casts in one go
- s is in scope only where the match is certain
- Works with && but not with ||
Pattern matching for instanceof previewed in Java 14 and 15 and became final in Java 16 (JEP 394).
Practice questions
What does this print?
Object o = 42;
if (o instanceof Integer i && i > 40) {
System.out.println("big " + (i + 1));
} else {
System.out.println("other");
}- big 43
- big 421
- other
- Compile error
Check your answer
big 43. o is an Integer, so i is bound. && only evaluates i > 40 when the match succeeded, so i can be used there.
What does this print?
String s = null;
System.out.println(s instanceof String);- true
- false
- Throws NullPointerException
Check your answer
false. instanceof returns false for null, regardless of the declared type. It never throws.