instanceof java keyword is used to check if an object is of some type or not. Java instanceof operator syntax is obj instanceOf Object
– left side is the instance and right side is the Class name. Java instanceof operator returns boolean result.
instanceof Java
In this section, let us see how instanceof
keyword can be used.
Let’s say we have some interfaces and class defined like below.
1 2 3 4 5 |
interface Vehicle { } class Car { } class Ferrari extends Car implements Vehicle {} |
If we check the instanceof
keyword with above class objects, we will see following results:
1 2 3 4 5 6 7 |
Ferrari ferrari = new Ferrari(); ferrari instanceof Vehicle // true - Ferrari implements Vehicle ferrari instanceof Car // true - Ferrari extends Car ferrari instanceof Ferrari // true - Ferrari is Ferrari ferrari instanceof Object // true - Object is the parent type of all objects |
But if we reverse something, we will see different results:
1 2 3 4 5 |
Car car = new Car(); car instanceof Ferrari // false - Car is super type of Ferrari, not other way around <img class="alignnone wp-image-30556 size-full" src="https://all-learning.com/wp-content/uploads/2017/11/instanceof-java1.png" alt="instanceof-java" width="1394" height="628" /> |
instanceof
is a useful tool when you’ve got a collection of objects and you’re not sure what they are.
instanceof operator vs isInstance() method
Basically, both of these do the same thing, check if an object is an instance of the same class as the other.
The instanceof operator works at compile time whereas the isInstance() method works at runtime. For example:
1 2 3 4 5 6 7 8 9 |
// This method tells us whether the object is an // instance of class whose name is passed as a // string 'c'. public static boolean checkType(Object obj, String c) throws ClassNotFoundException { return Class.forName(c).isInstance(obj); } |
Let us put above method in use to demonstrate its use:
1 2 3 4 5 |
String str = "Shubham"; checkType(str, "java.lang.String"); // true checkType(str, "java.lang.Object"); // true |
Java instanceof with array
Java array are also Object, but instanceof operator works differently when it’s used with array. For example;
1 2 3 |
new int[] {1} instanceof Object; // true |
But if we try something like below:
1 2 3 |
new String[]{"1"} instanceof String |
Then we get compile time error as Incompatible conditional operand types String[] and String
. However below use of instanceof operator works fine.
1 2 3 |
new String[]{"1"} instanceof String[] // true |
Java instanceof example
Here is a complete example for instanceof
java operator and isInstance
method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package com.journaldev.instanceofoperator; interface Vehicle {} class Car {} class Ferrari extends Car implements Vehicle {} public class InstanceOfExample { public static void main(String[] args) throws ClassNotFoundException { Car car = new Car(); Ferrari ferrari = new Ferrari(); String str = "Pankaj"; String[] strArray = { "1" }; System.out.println(ferrari instanceof Vehicle); // true - Ferrari implements Vehicle System.out.println(ferrari instanceof Car); // true - Ferrari extends Car System.out.println(ferrari instanceof Ferrari); // true - Ferrari is Ferrari System.out.println(ferrari instanceof Object); // true - Object is the parent type of all objects System.out.println(null instanceof Object); // false - null is not an Object System.out.println(car instanceof Ferrari); // false - Car is supertype of Ferrari, not other way around System.out.println(new int[] { 1 } instanceof Object); // true System.out.println(strArray instanceof String[]); // true System.out.println("*****"); System.out.println(checkType(str, "java.lang.String")); // true System.out.println(checkType(str, "java.lang.Object")); // true System.out.println(checkType(str, "com.journaldev.instanceofoperator.Vehicle")); // false System.out.println(checkType(strArray, "java.lang.Object")); // true System.out.println(checkType(strArray, "java.lang.String")); // false } public static boolean checkType(Object obj, String c) throws ClassNotFoundException { return Class.forName(c).isInstance(obj); } } |
That’s all for instanceof java operator.
Reference: Oracle Documentation