Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is the instanceOf() operator in Java?
3.
Example of Java Instanceof
4.
Uses of the Instanceof operator
4.1.
Applying Instanceof to a Variable with a Null Value
4.2.
Using Instanceof operator in Java for Downcasting
4.3.
Java
5.
Understanding Real use of instanceof in java
5.1.
Java
6.
Frequently Asked Questions
6.1.
What is the Instanceof method in Java?
6.2.
What is the function of the Instanceof method?
6.3.
What is the syntax of the Instanceof operator in Java?
6.4.
What is an example of an instance in Java?
6.5.
How to check instance type in Java?
7.
Conclusion
Last Updated: Mar 28, 2024
Easy

Java Instanceof Operator

Author Sanjana Yadav
0 upvote
Master Power BI using Netflix Data
Speaker
Ashwin Goyal
Product @
18 Jun, 2024 @ 01:30 PM

Introduction

In the world of computers, figuring out what kind of data you're dealing with can be similar. That's where the instanceof operator comes in Java! It acts like a detective, helping you identify exactly what type of object you're working with. 

InstanceOf() method in Java

So, let’s get started!

Also Read About  Iteration Statements in Java, and Duck Number in Java.

What is the instanceOf() operator in Java?

Instanceof operator in Java is used to determine whether a given object is an instance of a class, subclass, or interface. Due to its ability to compare the type and the instance, it is also known as the comparison operator. Generally, the instanceof operator in java only returns true or false. The instanceof operator is frequently used for the following :

  • Determining if a given variable has an instance of an object or not
  • Determining whether typecasting is legal or not
  • Downcasting (where the parent object is addressed by the subclass type)
Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Example of Java Instanceof

class InstanceofExample {
  public static void main(String args[]) {
    InstanceofExample ex = new InstanceofExample();
    System.out.println(ex instanceof InstanceofExample);// true
  }
}

 

Output

true

 

As seen in the example shown above, Java's instanceof keyword is used to determine whether the object ex is an instance of the InstanceofExample class. The program's result is true since object ex is a member of the InstanceofExample class.

Also see, Swap Function in Java

Uses of the Instanceof operator

Applying Instanceof to a Variable with a Null Value

When we use instanceof operator in java on a variable with a null value, the result is false. You can see how to utilize a variable with a null value with the instanceof operator in the code below. 

class InstanceofExample {
  public static void main(String args[]) {
    InstanceofExample ex = null;
    System.out.println(ex instanceof InstanceofExample);// false
  }
}

 

Output

false

Using Instanceof operator in Java for Downcasting

Downcasting is one of the main uses of the instanceof operator in java. It involves referring to a parent object with a subclass type. In layman's language, it refers to typecasting an object from a parent to a child. Although downcasting may be performed without the assistance of an instanceof operator, there is a high chance of running into compile-time or run-time errors. Therefore, it is best to use the Java instanceof operator to determine if downcasting is valid or not. The use of instanceof operator in java for downcasting is demonstrated in the example below.

  • Java

Java

class InstanceofExample { }

public class derived extends InstanceofExample {
 public static void func(InstanceofExample i) {
   if (i instanceof derived) {
     derived d = (derived) i;
     System.out.println("Downcasting has been performed.");
   }
 }
 
 public static void main(String args[]) {
   InstanceofExample i = new InstanceofExample();
   derived.func(i);
 }
}

Output

Downcasting has been performed.

Understanding Real use of instanceof in java

Let's say we make a parent class-type reference that points to a child class-type object. The data of the child object is what the parent reference wishes to access. Before we actually access the data, we may use instanceof to validate the object reference. Let us  look at the code below:

  • Java

Java

class Car {
 String sname = "Car";
}

class Mercedes extends Car {
 String sname = "Mercedes";
}

class ApplicationTest {
 public static void main(String[] args) {
   Car childClass = new Mercedes();
   Car parentClass = childClass;

   // checking the correctness of an instance before typecasting
   if (parentClass instanceof Mercedes) {
     System.out.println("The accessed class name is: "
         + ((Mercedes) parentClass).sname);
   }
 }
}

Output

The accessed class name is: Mercedes

 

Also see, Hashcode Method in Java

Frequently Asked Questions

What is the Instanceof method in Java?

The "instanceof" operator in Java is used to determine whether an object is an instance of the provided type (class or subclass or interface). Since it compares the instance to the type, it is also known as the type comparison operator.

What is the function of the Instanceof method?

The instanceof operator in java checks to see if a constructor's prototype property appears anywhere in an object's prototype chain. The result is a boolean value.

What is the syntax of the Instanceof operator in Java?

The syntax of the instanceof operator is: objectName instanceOf className. The operator returns true if objectName is an instance of className. If not, it returns false.

What is an example of an instance in Java?

An instance in Java is a real-world object created from a blueprint (class). Imagine a class for "Car" - an instance of that class could be your specific red Toyota Camry.

How to check instance type in Java?

Java uses the instanceof operator to check the type of an instance. 

Conclusion

In this article, we learned about the Java Instanceof Operator. The instanceof operator is your trusty detective in the world of Java objects! By using it, you can avoid surprises and ensure your code works smoothly. Remember, instanceof helps you check if an object belongs to a specific class or its subclasses. It's a simple but powerful tool that can keep your Java programs running like a well-oiled machine.


We hope this article has clarified your understanding of Instanceof() operator in Java. You can refer to our blogs to understand more about Java concepts.

 

You can also visit our website to read more such blogs. Make sure you enroll in our courses, take mock tests, solve problems, and interview puzzles. Also, you can prepare for interviews with interview experiences and an interview bundle.

Keep learning and keep growing, Ninjas!