Java is a versatile programming language that consists of a wide variety of methods, operators, and functions. These are utilized while developing applications in Java. In Java, there are different types of operators used, such as the arithmetic operator, the logical operator, the binary operator, etc. The instanceof operator in Java belongs to the binary operators that return the value of “true” or “false” according to the code.
In this article, we will elaborate on the following content for the instanceof operator in Java.
- What is the instanceof Operator in Java?
- The Basic Implementation of the instanceof operator
- Downcasting using the instanceof operator
- Object testing in Java using the instanceof operator
- Implementation of Null in instanceof operator
- Practical Application (Inheritance in Java)
- Bonus: Difference between instanceof operator and isInstance() Method
- Conclusion
What is the instanceof Operator in Java?
The instanceof operator in Java tests whether the specified/declared object belongs to the interface, subclass, or class in Java. This operator in Java also refers to the comparison operator since it compares the objects with the types. It returns a boolean value as “true” for a successful result otherwise “false” in Java.
The Basic Implementation of the instanceof Operator
The code below depicts the basic implementation of the instanceof operator in Java.
class Library{
//main() method of Java
public static void main(String args[]){
// create an object
Library books =new Library();
//Print using the operator
System.out.println("The Output is: " + (books instanceof Library));
}
}
In the above code block:
- The class is declared as “Library”.
- In the next step, within the main() method the object for the class is declared as “books”.
- The println() method prints the result using the instanceof operator to verify if “books” is an instanceof “Library”.
Output
The output below shows that the object “books” is an instance of the class “Library” in Java.
Downcasting Using the instanceof Operator
There is a need to understand typecasting before moving towards the main term of downcasting. In Java, typecasting refers to a process of converting one data type to another. Therefore, the term downcasting in Java refers to typecasting of the parent class(superclass) to the child class(subclass). The code below implements downcasting using the instanceof operator in Java.
class Library { }
//Extend the parent class
class Book extends Library {
static void expmethod(Library l) {
if(l instanceof Book){
Book b = (Book)l;
//print the output
System.out.println("The Downcasting is completed");
}
}
//main() method of Java
public static void main (String [] args) {
Library l=new Book();
Book.expmethod(l);
}
}
In the above code block:
- The superclass is declared as “Library”.
- The subclass “Book” extends the superclass “Library”.
- In a step forward, the if statement checks if the object of Libraray “l” is an instanceof “Book”.
- The downcasting is done when the subclass “Book” refers to the object “l” of the parent class.
- Within the main() method, the “Book.expmethod()” of the child class is invoked and the object of the parent class is passed to it as an argument to perform the downcasting.
- The output on the screen is printed using the println() method.
Output
The output shows that “downcasting” is completed using the instanceof operator in Java.
Checking Whether an Object is an Instance of the Class Using the instanceof Operator
The code below depicts how the instanceof operator in Java is verified.
class Method1{
//main() method of Java
public static void main(String args[]){
// create an object
Method1 m =new Method1();
//Print using the operator
System.out.println("The Output is: " + (m instanceof Method1));
}
}
In the above code block:
- The class is declared as “Method 1”.
- In the next step, within the main() method, the object for the class is declared as “m”.
- The println() method prints the result using the instanceof operator to verify if “m” is an instanceof “Method1”.
Output
The output below shows that the object “m” is an instance of the class “Method1” in Java.
Using instanceof Operator With Null Values in Java
The code below depicts the implementation of null while working with the instanceof operator in Java and returns “true” or “false” according to the condition.
class NullExample {
public static void main(String[] args)
throws java.lang.Exception
{ //Assign the values to the integer
String x = null;
System.out.println("The output for 'null' is " + (x instanceof String));
//The value is printed
String y = "Java";
System.out.println("The output for the String as 'Java' is " + (y instanceof String));
}
}
In the above Java code:
- In the main() method, the first string “x” is assigned a “null” value.
- The output is printed using the println() method with the instanceof operator.
- In the last part of the code, the second string “y” is declared as “Java” and the output is printed.
Output
The output shows that the instanceof the operator returns “false” for a null value. On the other hand, it returns “true” for the string.
Using instanceof Operator With Inheritance in Java
The instanceof operator can be used in inheritance to check whether the declared object is present in the superclass or the subclass, as depicted in the code below.
//Superclass
class Employee {
}
// subclass
class Id extends Employee {
}
class Main {
public static void main(String[] args) {
//Create an object of the subclass
Id i = new Id();
// check if i is the instance of the declared subclass
System.out.println("Is 'i' an instance of Id class? " + (i instanceof Id));
// check if i is the instance of the superclass
System.out.println("Is 'i' an instance of Employee class? " + (i instanceof Employee));
}
}
In the above code block:
- The superclass is declared as “Employee”.
- The subclass “Id” is inherited from the superclass “Employee”.
- In the next step, the main() method of Java contains an object for the subclass as “i”.
- The last part of the code contains the println() method that verifies if the object “i” is the instance of the superclass and the subclass.
Output
The output below shows that the object “i” of the subclass is an instance of the superclass and the subclass in Java.
Bonus Tip: instanceof Operator Vs isInstance() Method in Java
Both instanceof operator and isinstance() method are very much similar but the difference between the instanceof operator and the isinstance() method in Java occurs when we want to verify the object of the class dynamically. For this purpose, the isinstance() method is used in Java.
This brings an end to the implementation of the instanceof operator in Java.
Conclusion
The instanceof operator in Java checks if the declared object belongs to the interface, class, or subclass and returns a boolean value based on the code. The instanceof operator in Java is very useful while implementing inheritance in Java. In this article, we have implemented different scenarios to work with the instanceof operator in Java.