instanceof Java With Examples

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.


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:


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:


Car car = new Car();
car instanceof Ferrari // false - Car is super type of Ferrari, not other way around
instanceof-java

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:


// 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:


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;


new int[] {1} instanceof Object; // true

But if we try something like below:


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.


new String[]{"1"} instanceof String[] // true

Java instanceof example

Here is a complete example for instanceof java operator and isInstance method.


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

By admin

Leave a Reply

%d bloggers like this: