"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 4, lesson 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."