Java instanceof
Operator and Downcasting
In Java, instanceof is an operator which is used to check object reference. It checks whether the reference of an object belongs to the provided type or not. It returns either true or false, if an object reference is of specified type then it return true otherwise false.
We can use instanceof operator to check whether an object reference belongs to parent class, child class, or an interface.
It's also known as type comparison operator because it compares the instance with type.
Application of instanceof
Apart from testing object type, we can use it for object downcasting. However, we can perform downcasting using typecasting but it may raise ClassCastException at runtime.
To avoid the exception, we use instanceof operator. Doing this helps to perform casting.
When we do typecast, it is always a good idea to check if the typecasting is valid or not. instanceof helps us here. We can always first check for validity using instanceof, then do typecasting.
Syntax for declaring instanceof operator is given below.
Syntax
(object) instanceof (type)
object: It is an object reference variable.
type: It can be a class or an interface.
Time for an Example
Let's take an example to understand the use of instanceof
operator. Here we are testing the reference type is type of Test class and it returns true.
public class Test
{
public static void main(String[] args)
{
Test t = new Test();
System.out.println(t instanceof Test);
}
}
true
Example: Interface Reference Type
Lets create an interface and test reference type using instanceof operator. This operator returns if the reference object refers to right interface type. See the below example
interface Callable{
void call();
}
class Demo implements Callable {
public void call() {
System.out.println("Calling...");
}
public static void main(String[] args) {
Callable d = new Demo();
// Refer to a class
System.out.println((d instanceof Demo));
// Refer to an interface
System.out.println((d instanceof Callable));
}
}
true
true
If reference variable holds null then instanceof returns false. Lets see one more example.
class Demo {
public static void main(String[] args) {
Demo d = null;
System.out.println(d instanceof Demo);
}
}
false
Example : Inheritance
Lets take another example to understand instanceof operator, in which we created 5 classes and some of them extends to another class. Now by creating objects we are testing reference object belongs to which class.
class Parent{}
class Child1 extends Parent{}
class Child2 extends Parent{}
class Demo
{
public static void main(String[] args)
{
Parent p =new Parent();
Child1 c1 = new Child1();
Child2 c2 = new Child2();
System.out.println(c1 instanceof Parent); //true
System.out.println(c2 instanceof Parent); //true
System.out.println(p instanceof Child1); //false
System.out.println(p instanceof Child2); //false
p = c1;
System.out.println(p instanceof Child1); //true
System.out.println(p instanceof Child2); //false
p = c2;
System.out.println(p instanceof Child1); //false
System.out.println(p instanceof Child2); //true
}
}
true
true
false
false
true
false
false
true
Downcasting in Java
Downcasting is one of the advantages of using instanceof operator. Downcasting is a process to hold object of parent class in child class reference object. It is reverse of upcasting, in which object of child is assigned to parent class reference object.
However, we can perform downcasting using typecasting but it throws ClassCastException at run-time. So to avoid this we use instanceof operator to perform downcasting.
You can understand whole process by the below image.
Example of downcasting with instanceof
operator
class Parent{ }
public class Child extends Parent
{
public void check()
{
System.out.println("Sucessfull Casting");
}
public static void show(Parent p)
{
if(p instanceof Child)
{
Child b1=(Child)p;
b1.check();
}
}
public static void main(String[] args)
{
Parent p=new Child();
Child.show(p);
}
}
Sucessfull Casting