instanceof operator

"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.
4
Task
Java Core,  level 4lesson 1
Locked
Code entry
Sometimes you don't need to think, you just need to hammer it out! As paradoxical as it may seem, sometimes your fingers will "remember" better than your conscious mind. That's why while training at the secret CodeGym center you will sometimes encounter tasks that require you to enter code. By entering code, you get used to the syntax and assimilate some material. What's more, you combat laziness.

"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(7)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Sanjay Chauhan Level 28 , Delhi, India
2 March, 02:01
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, 02:37
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, 17:57
So true lol! The first 'a' should be highlighted instead 🤣CodeGym fix this please!
Ed Maphis Level 20 , Painesville, United States
3 June 2019, 16:02
So Tiger isPet. Nice.
Vahan Level 41 , Tbilisi, Georgia
2 September 2019, 06:50
Well, in the harsh country where the CodeGym secret lab is located, tigers are pets.
Agent Smith Level 27
26 August, 19:47
Pet tiger? Yawn. Check this out - https://youtu.be/IM0GEN_CNfI?t=40
Sergio Level 25 , Moscow, Russian Federation
24 April 2019, 20:48
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