- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
instanceof operator in Java
This operator is used only for object reference variables. The operator checks whether the object is of a particular type (class type or interface type). instanceof operator is written as −
( Object reference variable ) instanceof (class/interface type)
If the object referred by the variable on the left side of the operator passes the IS-A check for the class/interface type on the right side, then the result will be true. Following is an example −
Example
public class Test { public static void main(String args[]) { String name = "James"; // following will return true since name is type of String boolean result = name instanceof String; System.out.println( result ); } }
Output
This will produce the following result −
true
This operator will still return true if the object being compared is the assignment compatible with the type on the right. Following is one more example −
Example
class Vehicle {} public class Car extends Vehicle { public static void main(String args[]) { Vehicle a = new Car(); boolean result = a instanceof Car; System.out.println( result ); } }
Output
This will produce the following result −
true
- Related Articles
- Java instanceof operator
- instanceof operator vs isInstance method in java
- What is instanceof operator in Java? Explain.
- Instanceof operator in JavaScript
- How to use the instanceof operator in Java?
- instanceof Keyword in Java
- Java instanceof and its applications
- C++ equivalent of instanceof
- Ternary Operator in Java
- How to check "instanceof" class in Kotlin?
- The new operator in Java
- What is dot operator in Java?
- String compare by == operator in Java
- Importance of XOR operator in Java?
- List.replaceAll(UnaryOperator operator) method in Java

Advertisements