Java's instanceof operator is used to determine whether a given object is an instance of the class, subclass, or interface that is specified.
Because it compares the instance to the type, Java's instanceof operator is also known as the type comparison operator. It returns either true or false. When we use the instanceof operator on a variable that has a null value, it returns false.
// Java example instanceof operator
class Main{ public static void main(String args[]){ Main main=new Main(); System.out.println( main instanceof Main); } }
Output:
true
// Java example of instanceof operator
class Vehicels{} class Car extends Vehicels{ public static void main(String args[]){ Car car=new Car(); System.out.println( car instanceof Vehicels); } }
Output:
true
Downcasting occurs when the Subclass type refers to the object of the Parent class. If we do it directly, the compiler returns a Compilation error. If you typecast it, a ClassCastException is thrown at runtime. Downcasting is possible, however, if we use the instanceof operator.
Car car=new Vehicles(); //C.T error
When we typecast downcasting, a ClassCastException is thrown at runtime.
Car car=(Car)new Vehicles();//It will throw ClassCastException exception at run time
// Java example of instanceof operator
class Vehicels{} class Car extends Vehicels{ static void method(Vehicels vehicels) { if(vehicels instanceof Car){ Car car=(Car)vehicels; //downcasting performed System.out.println("Downcasting"); } } public static void main(String args[]){ Vehicels vehicels=new Car(); Car.method(vehicels); } }
Output:
Downcasting
// Example of instanceof operator
class Vehicles {} class Car extends Vehicles {} class Main { public static void main(String[] args) { Car car = null; if (car instanceof Car) System.out.println("car is an instance of Car Class"); else System.out.println("car is not an instance of Car Class"); if (car instanceof Vehicles) System.out.println("car is an instance of Vehicles Class"); else System.out.println("car is not an instance of the Vehicles Class."); if (car instanceof Object) System.out.println("car is an instance of Object"); else System.out.println("car is not instance of Object"); } }
Output:
car is not an instance of Car Class
car is not an instance of Vehicles Class
car is not an instance of Object
// Example of instanceof operator
class Vehicles {} class Car extends Vehicles {} class Main { public static void main(String[] args) { Car car = new Car(); if (car instanceof Car) System.out.println("car is an instance of Car Class"); else System.out.println("car is not an instance of Car Class"); if (car instanceof Vehicles) System.out.println("car is an instance of Vehicles Class"); else System.out.println("car is not an instance of the Vehicles Class."); if (car instanceof Object) System.out.println("car is an instance of Object"); else System.out.println("car is not instance of Object"); } }
Output:
car is an instance of Car Class
car is an instance of Vehicles Class
car is an instance of Object
Post your comment