
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is instanceof operator in Java? Explain.
The instanceof operator in Java is used to find whether a reference is an instance of a Type i.e. class or an interface.
Example
public class InstanceOfExample { public static void main(String args[]) { String str = "hello"; boolean bool = str instanceof String; System.out.println(bool); } }
Output
true
Legal operands of the instanceof operator
The following are the only legal operands of the instanceof operator −
- Left operand − It must be a reference representing an object.
- Right operand − It must be the name of Java class or, interface.
Except these two if you use any other operands a compile time error will be generated.
Example
public class InstanceOfExample { public static void main(String args[]) { int i =20; boolean bool = i instanceof String; System.out.println(bool); } }
Compile time error
InstanceOfExample.java:4: error: unexpected type boolean bool = i instanceof String; ^ required: reference found: int 1 error
- Related Questions & Answers
- Java instanceof operator
- instanceof operator in Java
- Instanceof operator in JavaScript
- instanceof operator vs isInstance method in java
- How to use the instanceof operator in Java?
- instanceof Keyword in Java
- What is dot operator in Java?
- What is narrowing in java? Explain.
- What is the conditional operator ?: in Java?
- Java instanceof and its applications
- Explain difference between == and is operator in Python.
- What is the difference between instanceof() and Array.isArray() methods in JavaScript?
- Explain exponentiation operator in JavaScript?
- Explain Grouping operator in JavaScript.
- What is @ operator in Python?
Advertisements