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
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.
Application of instanceof Keyword in Java
The instanceof keyword in Java is used to check whether an object is an instance of a specific class or subclass. It returns true if the object is of the specified type; otherwise, it returns false.
Applications:
- Type checking before casting: Helps prevent ClassCastException by verifying the object type before downcasting.
- Polymorphism handling: Used in conditions to execute different logic based on object type in inheritance hierarchies.
- Safe object comparison: Ensures the object is of the correct class before performing operations on it.
Example:
if (obj instanceof String) {
String str = (String) obj;
System.out.println("It is a String: " + str);
}

You can also try this code with Online Java Compiler
Run CodeThis makes instanceof a useful tool in runtime type-checking and object-oriented programming 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 instanceof and isinstanceof()?
instanceof is a Java keyword used for type checking. isinstanceof() is not a valid Java method; it may refer to similar functions in other languages like Python.
When to use instanceof?
Use instanceof when you need to verify an object's type before casting or executing type-specific logic, especially in polymorphic code or class hierarchies.
How to print instanceof in Java?
You can print the result of an instanceof check like this:
System.out.println(obj instanceof String);

You can also try this code with Online Java Compiler
Run CodeIt returns true if obj is a String, otherwise false.
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.
Recommended Readings:
Iteration Statements in Java
Duck Number in Java
Hashcode Method in Java
Upcasting and Downcasting in Java