Java instanceof Operator
In object-oriented programming, an object is defined as an instance of a class. The instanceof operator in Java, as the name suggests, is used to check this relationship. This operator is also known as Type Comparison Operator.
The instanceof operator returns a boolean value. If the object is of a particular type then this operator returns true, otherwise, it returns false. The syntax of this operator is shown below.
Syntax of instanceof
(object) instanceof (class or subclass or interface)
Example: instanceof in Java
The instanceof operator simply checks the is-a relationship between the object and the type. In the example shown below, we can say that an Apple is a fruit, but a car is a vehicle and not a fruit.
class Fruit{}
class Vehicle{}
public class InstanceofDemo
{
public static void main(String[] args)
{
Fruit pineapple = new Fruit();
Vehicle truck = new Vehicle();
System.out.println("pineapple IS A fruit: " + (pineapple instanceof Fruit));
System.out.println("truck IS A vehicle: " + (truck instanceof Vehicle));
}
}
pineapple IS A fruit: true
truck IS A vehicle: true
But if we run the following code, then we get a compilation error as there is no relation between a car and a fruit.
class Fruit{}
class Vehicle{}
public class InstanceofDemo
{
public static void main(String[] args)
{
Fruit apple = new Fruit();
Vehicle car = new Vehicle();
System.out.println("car IS A Fruit: " + (car instanceof Fruit));
}
}
Example: instanceof with subclass
Let's create a parent class and a child class to understand the relationship between them. An object of the subclass is also an instance of the parent class.
class A{}
class B extends A{}
public class InstanceofDemo
{
public static void main(String[] args)
{
B objOfClassB = new B();
System.out.println("objOfClassB is an instance of B: " + (objOfClassB instanceof B));
System.out.println("objOfClassB is an instance of the parent class A: " + (objOfClassB instanceof A));
}
}
objOfClassB is an instance of B: true
objOfClassB is an instance of the parent class A: true
A child class is a type of parent class but the opposite is not true. If we create an object of the parent class and compare it to the child class, then the instanceof operator will return false.
class A{}
class B extends A{}
public class InstanceofDemo
{
public static void main(String[] args)
{
B objOfClassB = new B();
A objOfClassA = new A();
System.out.println("objOfClassB is an instance of the parent class A: " + (objOfClassB instanceof A));
System.out.println("objOfClassA is an instance of the child class B: " + (objOfClassA instanceof B));
}
}
objOfClassB is an instance of the parent class A: true
objOfClassA is an instance of the child class B: false
Example: instanceof with interface
Also note that if the subclass implements an interface, then the instanceof operator returns true if we compare the object of the subclass with the interface.
class A{}
interface C{}
class B extends A implements C{}
public class InstanceofDemo
{
public static void main(String[] args)
{
B objOfClassB = new B();
System.out.println((objOfClassB instanceof B));
}
}
true
Example: instanceof with Object class
In Java, every class extends the Object class, and so any object will be an instance of the Object class. This can be verified by using the instanceof operator.
class A{}
interface C{}
class B extends A implements C{}
public class InstanceofDemo
{
public static void main(String[] args)
{
B objOfClassB = new B();
String str = "string";
Exception e = new Exception();
System.out.println((objOfClassB instanceof Object));
System.out.println((str instanceof Object));
System.out.println((e instanceof Object));
}
}
true
true
true
If the object is null then instanceof will always return false.
class A{}
interface C{}
class B extends A implements C{}
public class InstanceofDemo
{
public static void main(String[] args)
{
B objOfClassB = null;
System.out.println((objOfClassB instanceof B));
System.out.println((objOfClassB instanceof A));
System.out.println((objOfClassB instanceof C));
System.out.println((objOfClassB instanceof Object));
}
}
false
false
false
false
The instanceof operator can only be used with reified types(type whose information is available at runtime). We cannot use this operator for generic types as the generic type information is lost at runtime. The following code demonstrates this scenario. It will give a compilation error that says that the generic type information is erased at runtime and so the instanceof operator cannot work on it.
public static <T> boolean instanceCheck(List<T> list)
{
return list instanceof ArrayList<String>;
}
Cannot perform instanceof check against parameterized type ArrayList<String>. Use the form ArrayList<?> instead since further generic type information will be erased at runtime
Summary
The instanceof operator is simply used to check whether an object is an instance of or a type of a class or not. It is mostly used to check the object type before trying to access its data or methods. The instanceof operator is just an is-a relationship check. The instanceof operator will give a compilation error if the object and type are not related in any way, and it will always false if the object is null.