English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn about the Java instanceof operator in detail with the help of examples.
In Java, the instanceof keyword is a binary operator. It is used to check if an object is an instance of a specific class.
The operator also checks if the object is an instance of a class that implements an interface (to be discussed later in this tutorial).
The syntax of instanceof is:
result = objectName instanceof className;
The left operand of the instanceof operator is the object name, and the right operand is the class name. If the object is an instance of the class, the result is true; otherwise, it is false.
class Main { public static void main(String[] args) { String name = "w3codebox"; Integer age = 22; System.out.println("Is the name an instance of String: ",+ (name instanceof String)); System.out.println("age is an instance of Integer? ")+ (age instanceof Integer)); } }
Output:
Is name an instance of String? true Is age an instance of Integer? true
In the above example, we created a String type object name and another Integer type object age. Then, we used the instanceof operator to check whether the name is of String type and age is of Integer type.
In the case of inheritance, the instanceof operator is used to check whether the object of the subclass is also an instance of the superclass.
class Animal { } //Dog class is a subclass of Animal class Dog extends Animal { } class Main { public static void main(String[] args){ Dog d1 = new Dog(); //Check d1Whether it is an object of Dog System.out.println("d1Is it an instance of Dog? "+ (d1 instanceof Dog)); //Check d1Whether it is an object of Animal System.out.println("d1Is it an instance of Animal? "+ (d1 instanceof Animal)); } }
Output:
d1Is it an instance of Dog? true d1Is it an instance of Animal? true
In the above example, d1are instances of the Dog and Animal classes. Therefore, d1 instanceof Dog and d1 instanceof Animal are all true.
In Java, all classes inherit from the Object class, and inheritance of the Object class does not use the extends keyword. This inheritance occurs implicitly in Java.
class Animal { } class Dog { } class Cat { } class Main { public static void main(String[] args) { Dog d1 = new Dog(); Animal a1 = new Animal(); Cat c1 = new Cat(); System.out.println("d1Is it an instance of the Object class? "+ (d1 instanceof Object)); System.out.println("a1Is it an instance of the Object class? "+ (a1 instanceof Object)); System.out.println("c1Is it an instance of the Object class? "+ (c1 instanceof Object)); } }
Output:
d1Is it an instance of the Object class? true a1Is it an instance of the Object class? true c1Is it an instance of the Object class? true
In the above example, we have created objects a of the classes Animal, Dog, and Cat separately1、d1and c1. We have used the instanceof operator to check these objects a1, d1, c1Is it also an object of the Object class? The output is all true (true).
This is because the Object class is the root class defined in the java.lang package. All other classes are subclasses of the Object class that forms a hierarchical structure in Java.
In Java, the object of the subclass can be regarded as the object of the superclass. This is called upcasting.In plain language, it is to convert the object of the subclass to the object of the superclass. Here, the superclass object can be an interface.
The Java compiler automatically performs upcasting.
class Animal { public void displayInfo() { System.out.println("I am an animal."); } } class Dog extends Animal { } class Main { public static void main(String[] args) { Dog d1 = new Dog(); Animal a1 = d1; a1.displayInfo(); } }
Output:
I am an animal.
In the above example, we created an object d of the Dog class1. We use d1An object to create an object a of the Animal class1. This is called upcasting in Java.
This code executes without any problem. This is because upcasting is automatically completed by the Java compiler.
Downcasting is the opposite process of upcasting, that is, the opposite of upcasting, which is to convert the object of the superclass to the object of the subclass.
In the case of downcasting, the object of the superclass is treated as the object of the subclass. We must explicitly indicate the compiler to downcast in Java.
class Animal { } class Dog extends Animal { public void displayInfo() { System.out.println("I am a dog."); } } class Main { public static void main(String[] args) { Animal a1 = new Animal(); Dog d1 = (Dog)a1; // Downcasting d1.displayInfo(); } }
When the program runs, a ClassCastException named exception will be obtained. Let's see what happens here.
Here, we create an object a of the superclass Animal1. Then we try to cast a1The object is explicitly cast to a subclass Dog object d1.
This causes a problem. This is because the a1The object may also refer to other subclasses. If we create another subclass Cat along with Dog; the animal could be a cat or a dog, causing ambiguity.
To solve this problem, we can use the instanceof operator. Here's how to do it:
class Animal { } class Dog extends Animal { public void displayInfo() { System.out.println("I am a dog"); } } class Main { public static void main(String[] args) { Dog d1 = new Dog(); Animal a1 = d1; // Upcasting if (a1 instanceof Dog) { Dog d2 = (Dog)a1; // Downcasting d2.displayInfo(); } } }
Output:
I am a dog
In the above example, we use the instanceof operator to check a1Whether an object is an instance of the Dog class. Only when the expression a1 Type downcasting is performed only when instanceof Dog is true.
The instanceof operator is also used to check if an object of a class is also an instance of an interface implemented by that class.
interface Animal { } class Dog implements Animal { } class Main { public static void main(String[] args) { Dog d1 = new Dog(); System.out.println("d1Is the object an instance of Animal: "+(d1 instanceof Animal)); } }
Output:
d1Is the object an instance of Animal: true
In the above example, we created a class that implements the Animal interface as Dog.
Then, create an object of the Dog class d1We have used the instanceof operator to check d1Is the object also an instance of the Animal interface?