CodeGym /Courses /Module 2. Java Core /instanceof operator

instanceof operator

Module 2. Java Core
Level 7, Lesson 0
Available

"Hello, Amigo! You've already met the instanceof operator. Today I'm going to tell you how and where it can be used. instanceof is a very simple and efficient operator."

"That sounds like an ad!"

"It really is very simple. It's used like this: «object» instanceof «class»."

It checks whether an object is an instance of a particular class. It's easier than explaining it. Look at this example:

Code Description
Object o = new Integer(3);
boolean isInt = o instanceof Integer;
isInt will be true. The object referenced by the variable o is an instance of the Integer class.
Object o = "Mama";
boolean isInt = o instanceof Integer;
isInt will be false. The object referenced by the variable o is not an instance of the Integer class. It is a String object.
InputStream is = new FileInputStream("");
boolean isFIS = is instanceof FileInputStream;
isFIS will be true. The object referenced by the variable o is an instance of the FileInputStream class.

"Yes, that is very simple."

"This operator also accounts for inheritance. Check it out."

Code Description
class Animal
{
}
class Cat extends Animal
{
}
class Tiger extends Cat
{
}
Here we have three class declarations: Animal, Cat, and Tiger. Cat inherits Animal. And Tiger inherits Cat.
Object o = new Tiger();
boolean isCat = o instanceof Cat;
boolean isTiger = o instanceof Tiger;
boolean isAnimal = o instanceof Animal;
isCat will be true.
isTiger will be true.
isAnimal will be true.
Object o = new Animal();
boolean isCat = o instanceof Cat;
boolean isTiger = o instanceof Tiger;
boolean isAnimal = o instanceof Animal;
isCat will be false.
isTiger will be false.
isAnimal will be true.

And even interfaces:

Code Description
interface Moveable
{
}
class Cat
{
}
class TomCat extends Cat implements Moveable
{
}
Create two classes: Cat, TomCat and the Moveable interface
Cat o = new TomCat();
boolean isCat = o instanceof Cat;
boolean isMoveable = o instanceof Moveable;
boolean isTom = o instanceof TomCat;
isCat will be true.
isMoveable will be true.
isTom will be true.
Cat o = new Cat();
boolean isCat = o instanceof Cat;
boolean isMoveable = o instanceof Moveable;
boolean isTom = o instanceof TomCat;
isCat will be true.
isMoveable will be false.
isTom will be false.

The instanceof operator looks like this: a instanceof B.

In other words, the instanceof operator will return true if:

1) variable a stores a reference to an object of type B

2) variable a stores a reference to an object whose class inherits B

3) variable a stores a reference to an object that implements interface B

Otherwise, the instanceof operator will return false.

"Got it. So why is this necessary, Uncle Rishi?"

"Ellie's going to tell you about that today. This is a really nice operator. You'll be convinced of that today."

Comments (10)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Evgeniia Shabaeva Level 42, Budapest, Hungary
8 July 2024
So, we can use the formulae: - «object» instanceof «class»; - «object» instanceof «parent class»; - «object» instanceof «interface».
Godfrey Ouma Level 1, Nairobi, Kenya Expert
21 October 2024
Yes, you have hit the nail on the head.
Karas Level 3, Tampa, United States
24 November 2020
I think this explanation should be a little bit earlier in the course, I remember flipping my desk as there where some exercises and it was always printing a cat when shold be printing a tigger. It also had to do something with the order in which it resolves, will it check for the object itself first or the inherited class?
Sanjay Chauhan Level 28, Delhi, India
2 March 2020
if you don't understand this article very well then refer the below article:

https://codegym.cc/groups/posts/105-how-the-instanceof-operator-works
Jason Level 26, Rancho Cucamonga, United States
21 August 2019
I'm pretty sure in examples 2 and 3 the wrong "a"s are highlighted in red.
Angel Li Level 18, Fremont, United States
4 July 2020
So true lol! The first 'a' should be highlighted instead 🤣CodeGym fix this please!
Ed Maphis Level 20, Painesville, United States
3 June 2019
So Tiger isPet. Nice.
Vahan Level 41, Tbilisi, Georgia
2 September 2019
Well, in the harsh country where the CodeGym secret lab is located, tigers are pets.
Agent Smith Level 38
26 August 2020
Pet tiger? Yawn. Check this out - https://youtu.be/IM0GEN_CNfI?t=40
Sergio Level 25, Moscow, Russian Federation
24 April 2019
Here we have three class declarations: Animal, Cat, and Tiger. Cat inherits Animal. And Tiger inherits Cat. Name of the class should be Pet not Animal in this description, if we reference the code snippet