Skip to main content

How does the instanceof keyword work?

Junior Java
Quick Answer instanceof checks if an object is an instance of a class or implements an interface at runtime. Returns true if the object is the specified type or a subtype. Usage: if (obj instanceof String s) (Java 16 pattern matching - casts and binds in one step). Use before downcasting to avoid ClassCastException. instanceof on null always returns false.

Answer

Checks whether an object belongs to a specific class or subclass.
Useful for safe type casting.
S
SugharaIQ Editorial Team Verified Answer

This answer has been peer-reviewed by industry experts holding senior engineering roles to ensure technical accuracy and relevance for modern interview standards.

Want to bookmark, take notes, or join discussions?

Sign in to access all features and personalize your learning experience.

Sign In Create Account

Source: SugharaIQ

Ready to level up? Start Practice