How to Use InstanceOf Operator in Java

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: When to Use Static Methods in Java

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
Your next lesson will play in 10 seconds
  • 0:04 What Am I?
  • 1:09 Object Testing
  • 1:45 Downcasting
  • 2:44 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Sometimes we need to know what data type an object is. This lesson explains the instanceof keyword in Java and provide code examples of its use and function.

What Am I?

There are times when we need to know what data type a given object is, or if an object is a certain type (e.g., if Apple is a type of Pie or Tree). The Java instanceof keyword is used for these tests; it compares the object with a given type. Instanceof returns only TRUE or FALSE.

It helps to see the code in action, so let's look at some code samples so we can see how this keyword is used. Below is an example of the keyword. We've declared a String object, then use instanceof to determine if the object is a string.

public static void main(String[] args) {
  String what = "What Am I?";
  if(what instanceof java.lang.String) {
   System.out.println(what = " is a String");

And the output:

java instanceof basic output

An Important Note About a Null Value!

If the variable or object is NULL (no value), instanceof will return FALSE. It doesn't matter if the variable is really an instance of a Pie; if it has somehow been changed to NULL, your test will fail.

Here's another useful test for instanceof. Let's say we created an array of Strings instead of a standard String. The object is no longer a String and instanceof will return false. If we want to check if an object is a String array, we can use the keyword as follows:

public static void main(String[] args) {
  String[] myString = {
  if(myString instanceof String[]) {
   System.out.println("Object is String Array&quot);

And the output:

Java instanceof test for array output

Object Testing

Now, it's pretty obvious that our object is a String! But remember that Java is object-oriented, and instanceof can be very helpful when testing our own classes.

Let's say we have a class called Rhubarb and we create a new instance of it. To check if the class is really an instance of Rhubarb, we can use the following:

Rhubarb r = new Rhubar();
if(r instanceof Rhubarb) {

For the most part, you will know that Rhubarb is an object of type Pie. But what about Birch? Is it a child class of Tree or BoysName? Below is a snippet from a Java program that checks to make sure that Raptor is really an object of the Dinosaur class (as opposed to a vehicle or some other class).

System.out.println(raptor instanceof Dinosaur);

Another use for instanceof is when we are downcasting objects.

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account