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
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);
}
}

You can also try this code with Online Java Compiler
Run Code
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
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);
}
}
}

You can also try this code with Online Java Compiler
Run Code
Output
The accessed class name is: Mercedes
Also see, Hashcode Method in Java
Downcasting with Java instanceof Operator
In Java, downcasting is the process of converting a superclass reference to a subclass type. However, downcasting directly can lead to ClassCastException if the object isn’t of the expected subclass type. The instanceof operator is commonly used to prevent this issue by checking an object’s type before attempting a downcast.
Example:
Animal animal = new Dog(); // Upcasting
if (animal instanceof Dog) {
Dog dog = (Dog) animal; // Safe downcasting
dog.bark();
}
In this example, the instanceof check ensures that animal is an instance of Dog before downcasting, making it safe to access Dog-specific methods like bark().
Downcasting Without the Use of Java instanceof
While instanceof is the preferred way to check an object’s type, it’s possible to downcast without it. This approach assumes you’re certain of the object’s type, typically in cases where external factors (like context or design patterns) guarantee it. However, without instanceof, an incorrect downcast will cause a ClassCastException at runtime if the object isn’t of the expected type.
Example:
Animal animal = new Dog(); // Upcasting
Dog dog = (Dog) animal; // Direct downcast without instanceof
dog.bark();
Here, we downcast directly, assuming animal is always of type Dog. While concise, this approach can be risky if the object’s type isn’t guaranteed, as it lacks the safety check provided by instanceof.
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.