What is the 'instanceof' operator used for in Java?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Java, the instanceof operator is a powerful tool used to test whether an object is an instance of a specific class or interface. This operator serves as a means of type-checking at runtime, helping developers ensure that an object conforms to a required type before performing operations that require that particular type.
Technical Explanation
The instanceof operator is used to check the relationship between an object and a class or interface. It is a binary operator that requires the syntax:
Here, object is the reference for which the type-check needs to be performed, and ClassName is the name of the class or interface against which the object is being checked. The expression returns a boolean value — true if the object is an instance of the specified class or its subclasses, or if the object implements that interface; false otherwise.
Key Points
- Type Safety:
instanceofensures that operations are performed only on objects of compatible types, thus preventingClassCastExceptionat runtime. - Interfaces: Objects can be checked to see if they implement specific interfaces using
instanceof. - Null Handling: If the object being tested is
null,instanceofwill always returnfalse. - Multiple Class Levels: In Java’s inheritance hierarchy,
instanceofchecks extend from superclasses to subclasses and implemented interfaces.
Examples
Consider the following example which highlights the use of instanceof in different scenarios:
In this example:
- The expression
dog instanceof Dogyieldstruebecausedogis an instance ofDog. dog instanceof AnimalistruebecauseDogis a subclass ofAnimal.- Checking
animal instanceof Dogresults infalseasAnimalis the superclass. - The
dog instanceof PetoutputstrueassumingDogimplements thePetinterface. - As expected,
null instanceofchecks always returnfalse.
Performance Considerations
While instanceof is a valuable tool for type-checking, excessive use can incur performance costs, especially in dynamic polymorphic behavior where type-checking logic instead of polymorphic dispatch might dominate. Therefore, its usage should be prudent and meaningful to avoid type-checking becoming a bottleneck.
Summary Table
Below is a table summarizing key points about the instanceof operator:
| Aspect | Description |
| Type Safety | Ensures objects are of a compatible type. |
| Null Handling | Returns false for null object checks. |
| Hierarchy Checks | Evaluates objects against class hierarchies & interfaces. |
| Performance | Excessive use may affect performance. |
| Syntax | object instanceof ClassName |
In conclusion, the instanceof operator is invaluable for performing type checks, providing a safe way to interact with various objects within Java’s type system. Its usage can improve code reliability by preventing runtime errors related to invalid typecasting. However, understanding and managing its performance implications ensures that while employing such checks, applications remain efficient and responsive.
Related reading
- What is the Java ? operator called and what does it do?
- What is the Java equivalent for LINQ?
- What is the LIMIT clause alternative in JPQL?
- What is the list of valid @SuppressWarnings warning names in Java?
- What is the maven-shade-plugin used for, and why would you want to relocate Java packages?
- What is the meaning of double tilde in Java?
- What is the meaning of serial thread-confinement when writing parallel algorithms in java?
- What is the meaning of the CascadeType.ALL for a @ManyToOne JPA association

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.