Instanceof Java Example – How to use Instanceof
In this example, we will show how to use the instanceof Java operator. 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 java 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 instanceof Java operator
The operator instanceof
has the following general form:
1 | 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 on 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 the Java operator instanceof
Create a java class named InstanceofExample.java
with the following code:
InstanceofExample.java
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | 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 an 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. How to use instanceof
Let us look at another example of the usage of instanceof. Consider a scenario where a user has multiple accounts and we need to provide a feature only for a type of account. Account
interface is implemented by SavingsAcct
and HomeLoanAcct
classes.
Account.java
/* * Account interface */ public interface Account{ public void baseInterestRate(); public String accountType(); public String getName(); }
SavingsAcct.java
public class SavingsAcct implements Account{ private String name; public void baseInterestRate(){ System.out.println("Savings account interest rate of 6% for account : "+name); } public String accountType(){ return "Savings"; } public SavingsAcct(String name){ this.name = name; } public String getName(){ return this.name; } }
HomeLoanAcct.java
public class HomeLoanAcct implements Account{ private String name; public void baseInterestRate(){ System.out.println("Home account interest rate of 8% for account :"+name); } public String accountType(){ return "Home Loan"; } public HomeLoanAcct(String name){ this.name = name; } public String getName(){ return this.name; } }
Let us now build the logic to identify and perform some action only for Savings account.
AccountTypeSample.java
import java.util.List; import java.util.ArrayList; public class AccountTypeSample{ public static void main(String args[]){ // Create a list to hold accounts List accountList = new ArrayList(); // create accounts Account savingAcct1 = new SavingsAcct("Savings1"); Account savingAcct2 = new SavingsAcct("Savings2"); Account homeLoanAcct1 = new HomeLoanAcct("Loan1"); Account homeLoanAcct2 = new HomeLoanAcct("Loan2"); // add accounts to list accountList.add(savingAcct1); accountList.add(savingAcct2); accountList.add(homeLoanAcct1); accountList.add(homeLoanAcct2); // a business logic to be applied only for particular type of accounts for(Account acct : accountList){ if(acct instanceof SavingsAcct){ System.out.println("account "+acct.getName()+" is of type :"+acct.accountType()); } } } }
In the above example, we create objects for Savings and Home loan accounts. We then identified the Saving accounts using instanceof
operator. Executing the class provides output as below
account Savings1 is of type :Savings account Savings2 is of type :Savings
3. Download the source code
This was an example of using the operator instanceof
in Java.
Download the source code here: Instanceof Java Example – How to use Instanceof
Last updated on Mar. 04th, 2020