Home » Java Basics » operators » Java instanceof Example – How to use instanceof

About Konstantina Dimtsa

Konstantina Dimtsa
Konstantina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she is currently pursuing M.Sc studies in Advanced Information Systems at the same department. She is also working as a research associate for NKUA in the field of telecommunications. Her main interests lie in software engineering, web applications, databases and telecommunications.

Java instanceof Example – How to use instanceof

In this example we will show how to use the operator instanceof in Java.This operator is a Type Comparison Operator and can be used when we want to check if an object is an instance of a specific class, an instance of a subclass, or an instance of a class that implements a particular interface. The instanceof operator compares an object to a specified type and returns true if the type of object and the specified type are the same.

1. Description of the operator instanceof

The operator instanceof has the following general form:
 

object instanceof type

In the above expression, object is an instance of a class, and type is a class type. If object is of the specified type in the right side of the expression or can be cast into that type, then the instanceof operator returns true, otherwise it returns false. Also, instanceof operator returns false if the object in the left side of the expression is null, no matter what the type is, because null is not an instance of anything.

2. Examples of using operator instanceof

Create a java class named InstanceofExample.java with the following code:

InstanceofExample.java

public class InstanceofExample {
	public static void main(String[] args) {

		Vehicle vehicle = new Vehicle();
		Car car = new Car();
		MotorCycle moto = new MotorCycle();

		// Those will evaluate to true
		System.out.println("vehicle instanceof Vehicle: "
				+ (vehicle instanceof Vehicle));
		System.out.println("car instanceof Vehicle: "
				+ (car instanceof Vehicle));
		System.out.println("car instanceof Car: " + (car instanceof Car));
		System.out.println("car instanceof DriveCar: "
				+ (car instanceof DriveCar));
		System.out.println("moto instanceof Vehicle: "
				+ (moto instanceof Vehicle));
		System.out.println("moto instanceof MotorCycle: "
				+ (moto instanceof MotorCycle));

		// those will evaluate to false
		System.out.println("vehicle instanceof Car: "
				+ (vehicle instanceof Car));
		System.out.println("vehicle instanceof DriveCar: "
				+ (vehicle instanceof DriveCar));
		System.out.println("moto instanceof DriveCar: "
				+ (moto instanceof DriveCar));

		// those will evaluate to false, as the object car is null
		car = null;
		System.out.println("(car=null) instanceof Vehicle: "
				+ (car instanceof Vehicle));
		System.out.println("(car=null) instanceof Car: " 
				+ (car instanceof Car));
	}

}

class Vehicle {
}

class Car extends Vehicle implements DriveCar {
}

class MotorCycle extends Vehicle {
}

interface DriveCar {
}

As we see in the above code, except for the main class, we have also defined three classes and one interface. We have defined a parent class called Vehicle and two subclasses called Car and MotorCycle, perspectively. Also, we have defined an interface called DriveCar, which is implemented only by class Car. Then we use the operator instanceof in different cases so as to check the hierarchy among the different types of classes.

  • Output:
vehicle instanceof Vehicle: true
car instanceof Vehicle: true
car instanceof Car: true
car instanceof DriveCar: true
moto instanceof Vehicle: true
moto instanceof MotorCycle: true
vehicle instanceof Car: false
vehicle instanceof DriveCar: false
moto instanceof DriveCar: false
(car=null) instanceof Vehicle: false
(car=null) instanceof Car: false

Let’s give a short explanation of the output. Instances car and moto are also of type Vehicle due to hierarchy, so these assumptions are true. However, vehicle is not instance of Car (neither MotorCycle of course). Also, the instances vehicle and moto are not instances of DriveCar, as only car is an instance of that type. Finally, when the car gets the null value, is not an instance of Vehicle or Car anymore.

3. Download the source code

This was an example of using the operator instanceof in Java. Download the source code here: InstanceofExample.zip

(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of