Interview Q&A · Java · 5 questions in this topic
instanceof and null — Java Interview Q&A
What the instanceof check guarantees and what a null reference really means. Part of OOP Mechanics · 294 questions in the Java bank.
What does the instanceof operator do?
Answer
The instanceof operator tests the runtime type of an object and returns true when the reference can be cast to the target type. The syntax is reference instanceof Type, and the result is false when the reference is null instead of throwing an exception. The compiler rejects checks between unrelated types with an incompatible-types error, so only plausible casts compile. Since Java 16, pattern matching form such as if (obj instanceof String s) both tests and casts in one step.
Key points
- 1Returns false for null rather than throwing.
- 2Requires a plausible subtype relationship or compilation fails.
- 3Pattern variables from Java 16 remove the explicit cast.
- 4Because null always fails the test, instanceof doubles as a null-safe guard before a cast.
- 5A check against an interface always compiles, since any class could implement it at runtime.
Example
Object x = "hello"; if (x instanceof String s) { System.out.println(s.toUpperCase()); }
What does null mean in Java?
Answer
null is a literal meaning a reference variable points to no object at all. Calling a method or reading a field through a null reference throws a NullPointerException at runtime. Only reference types can be null, while primitives such as int and boolean always hold a value and can never be null.
Example
Employee e = null; if (e == null) { System.out.println("no object yet"); }
What does instanceof return for a null reference?
Answer
false, always, and it never throws. The operator asks whether the reference points at an instance of that type, and null points at no instance, so the answer is false for every type including Object. That makes instanceof a null-safe check, unlike a cast or a method call, which is why the null check can be folded into the type test instead of being written separately.
Key points
- 1null instanceof AnyType is false.
- 2No exception is thrown for the null case.
- 3The check is null-safe; the following cast is not needed separately.
- 4null is assignable to any reference type but is an instance of none.
What is pattern matching for instanceof?
Answer
Since Java 16 you can bind the cast in the same expression: if (obj instanceof String s) declares s as a String for the branch where the test succeeded. The variable is scoped by flow analysis, so it is in scope after a positive test and also in the else branch of a negated test. It removes the redundant cast and the possibility of the two getting out of sync.
Key points
- 1Binding form avoids a separate cast.
- 2Scope follows flow analysis, including negated conditions.
- 3Combining with && is allowed once the pattern has matched.
- 4switch can use the same type patterns since Java 21.
Example
if (obj instanceof String s && s.length() > 3) { System.out.println(s.toUpperCase()); // s is in scope here }
Do you still need a null check before using the result of a cast?
Answer
instanceof already excludes null, so a cast guarded by it cannot fail on null. What you still need to handle is the case where the test is false and the code takes another path — and any values obtained afterwards that were not covered by the test. The subtle bug is assuming that a successful instanceof guarantees a field inside the object is non-null, which is a different question entirely.
Key points
- 1A cast inside a matched instanceof branch is safe for that value.
- 2The false branch still needs an explicit decision.
- 3instanceof says nothing about the object’s fields.
- 4Optional is the idiomatic carrier for “may be absent”.
Learn this topic
Recommended resourcesAffiliate links · may earn us a commission