instanceof Operator in Java
- instanceof returns false for null — it never throws NullPointerException.
- Pattern matching instanceof (Java 16+) combines the type check and cast into one expression.
- instanceof checks the actual runtime type, not the declared type of the variable.
instanceof checks whether an object is an instance of a given class or interface at runtime. It returns true if the object is that type or a subtype, and false if the object is null. Java 16 added pattern matching: if (obj instanceof String s) — this checks the type and casts in one expression.
Basic instanceof Check
instanceof evaluates at runtime using the JVM's type system. It returns true if the object's actual class is the specified type or any subtype of it.
package io.thecodeforge.java.operators; public class InstanceofBasics { public static void main(String[] args) { Object text = "Hello, Forge"; Object number = 42; Object nothing = null; System.out.println(text instanceof String); // true System.out.println(number instanceof String); // false System.out.println(nothing instanceof String); // false — null is never instanceof anything // Subtype check Number n = 3.14; System.out.println(n instanceof Number); // true System.out.println(n instanceof Double); // true — Double extends Number System.out.println(n instanceof Integer);// false } }
false
false
true
true
false
Pattern Matching instanceof — Java 16+
Before Java 16, you had to cast explicitly after an instanceof check — verbose and error-prone. Pattern matching collapses the check and cast into one expression.
package io.thecodeforge.java.operators; public class PatternMatching { // Old style — before Java 16 static String describeOld(Object obj) { if (obj instanceof String) { String s = (String) obj; // manual cast return "String of length " + s.length(); } else if (obj instanceof Integer) { Integer i = (Integer) obj; return "Integer: " + (i > 0 ? "positive" : "non-positive"); } return "Unknown: " + obj.getClass().getSimpleName(); } // Pattern matching — Java 16+ (binding variable declared inline) static String describe(Object obj) { if (obj instanceof String s) { return "String of length " + s.length(); // s is in scope here } else if (obj instanceof Integer i && i > 0) { return "Positive integer: " + i; // can use binding var in condition } else if (obj instanceof Integer i) { return "Non-positive integer: " + i; } return "Unknown: " + obj.getClass().getSimpleName(); } public static void main(String[] args) { System.out.println(describe("TheCodeForge")); // String of length 12 System.out.println(describe(42)); // Positive integer: 42 System.out.println(describe(-5)); // Non-positive integer: -5 System.out.println(describe(3.14)); // Unknown: Double } }
Positive integer: 42
Non-positive integer: -5
Unknown: Double
instanceof with Interfaces
instanceof works with interfaces too. An object passes the instanceof check if its class implements the interface, directly or through a superclass.
package io.thecodeforge.java.operators; import java.util.ArrayList; import java.util.List; public class InterfaceCheck { public static void main(String[] args) { List<String> list = new ArrayList<>(); System.out.println(list instanceof List); // true System.out.println(list instanceof ArrayList); // true System.out.println(list instanceof Iterable); // true — List extends Iterable // Useful pattern: safe processing Object[] items = {"hello", 42, null, new ArrayList<>()}; for (Object item : items) { if (item instanceof Iterable<?> it) { System.out.println("Iterable found: " + it.getClass().getSimpleName()); } else if (item instanceof String s) { System.out.println("String: " + s.toUpperCase()); } else { System.out.println("Other: " + item); } } } }
true
true
String: HELLO
Other: 42
Other: null
Iterable found: ArrayList
🎯 Key Takeaways
- instanceof returns false for null — it never throws NullPointerException.
- Pattern matching instanceof (Java 16+) combines the type check and cast into one expression.
- instanceof checks the actual runtime type, not the declared type of the variable.
- A subtype always passes an instanceof check for its parent type.
- Overusing instanceof often means polymorphism would be cleaner — consider virtual dispatch instead.
Interview Questions on This Topic
- QWhat does instanceof return when the reference is null?
- QWhat is pattern matching instanceof and which Java version introduced it?
- QWhat is the difference between instanceof and getClass() for type checking?
Frequently Asked Questions
Can instanceof throw an exception?
No. instanceof never throws at runtime. If the reference is null, it simply returns false. If there is a type mismatch the compiler would catch at compile time, you would get a compile error — not a runtime exception.
What is the difference between instanceof and getClass() == ?
instanceof returns true for the object's class AND all its supertypes. getClass() == SomeClass.class returns true only for the exact class, not subtypes. For most use cases instanceof is what you want. Use getClass() equality when you specifically need to exclude subclasses.
Does instanceof work with generics?
Only partially. You can write obj instanceof List<?> but not obj instanceof List<String>. Generic type information is erased at runtime — the JVM only knows it is a List, not what type it contains. The wildcard <?> is required to make the compiler accept the expression.
Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.