HuntExams Academy logo
JavaBeginner#fundamentals

What is the instanceof operator?

Answer

It checks whether an object is an instance of a specific class or implements a given interface, returning a boolean, and is often used before casting to avoid ClassCastException.

Example

Object obj = "hello";
if (obj instanceof String s) { // pattern matching (Java 16+)
  System.out.println(s.length());
}

Detailed explanation

The instanceof operator in Java tests whether a reference points to an object compatible with a specified reference type and returns a boolean. The type can be a class or an interface. Compatibility includes inheritance: an object of a subclass is also an instance of its superclass and any interfaces it implements, directly or through inheritance. For example, if Object obj = "hello", both obj instanceof String and obj instanceof CharSequence return true because String implements CharSequence. It does not check only for an exact class match.

The result depends on the actual object at runtime, not just the variable’s declared type. In this example, obj is declared as Object, but it refers to a String. The JVM uses runtime type information to determine whether the object is compatible with the target type; it does not create a new object or convert the existing one. A null reference always produces false when tested with instanceof. The operator works with reference types, not primitive values in ordinary Java 16-style usage, and the compiler rejects checks between types that cannot possibly be compatible, such as a String reference checked against Integer.

A common use is guarding a downcast. Before pattern matching, you would write if (obj instanceof String) { String s = (String) obj; System.out.println(s.length()); }. Since Java 16, pattern matching allows if (obj instanceof String s) { System.out.println(s.length()); }. When the test succeeds, s refers to the same object, now available through a String-typed variable without an explicit cast. Its scope is flow-sensitive: Java allows access only where the match is known to have succeeded. This also supports conditions such as obj instanceof String s && s.length() > 0.

Interviewers usually expect you to explain runtime type checking, null handling, inheritance, interfaces, and safe casting. A successful check protects the corresponding cast from ClassCastException, provided you cast the same reference value to the checked type. However, repeated type checks across a class hierarchy can indicate that polymorphism would be clearer: calling an overridden method often avoids manually selecting behaviour based on each object’s type.

Key points to remember

  • instanceof returns a boolean indicating runtime compatibility with a reference type.
  • It recognises superclass relationships and implemented interfaces, not just exact class matches.
  • null instanceof SomeType always evaluates to false.
  • A successful check can guard a downcast against ClassCastException.
  • Java 16 finalised pattern matching for instanceof, combining a type test with a typed variable.

Common mistakes candidates make

  • Assuming instanceof checks only the exact class or only the variable’s declared type.
  • Expecting a null reference to cause NullPointerException during an instanceof check.
  • Using a pattern variable outside the scope where Java knows its type test succeeded.

Likely follow-up questions

How does instanceof differ from getClass()?

instanceof tests type compatibility, including inheritance and interfaces, whereas obj.getClass() == String.class tests an exact runtime class. Calling getClass() on null throws NullPointerException, while instanceof returns false.

Can instanceof check generic types?

You cannot check obj instanceof List<String> because type erasure means the runtime generally cannot verify the element type. You can check obj instanceof List<?>, but that only establishes that the object is a List, not that its elements are strings.

Why does a pattern variable work with && but usually not with ||?

With obj instanceof String s && s.length() > 0, the right operand runs only after the type test succeeds, so s is available. With ||, the right operand runs when the test fails, so s is not available there.

Interview tip: Define the operator, demonstrate the supplied String example, and finish by mentioning null handling and how pattern matching removes the explicit cast.

Related Interview Questions

1
JavaBeginner#fundamentals#java10

What is the difference between var and explicit typing in Java 10+?

Open
2
JavaIntermediate#inner-classes

What is the difference between a static nested class and a non-static inner class?

Open
3
JavaIntermediate#inner-classes

What are anonymous inner classes used for?

Open