← Back to Core Java Course | Chapter 7: OOP Core | Lesson 10 of 11

Java instanceof Operator

instanceof asks whether an object is a certain type, like checking whether an animal is a dog. It answers true or false.
Syntax
java
if (object instanceof ClassName) {
    // object is a ClassName
}
if (object instanceof ClassName name) {
    // pattern matching: name is already cast
}

What is the instanceof Operator?

instanceof tests whether an object is an instance of a given class or interface at runtime, returning a plain boolean — it's the standard way to check an object's actual type before performing a type-specific operation, especially with polymorphic collections.

Example: What is the instanceof Operator?

java
// Define the class Animal
class Animal {}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new Animal object and store it in a
		Animal a = new Animal();
		// Print a line to the console
		System.out.println(a instanceof Animal);
	}
}

Inheritance Checks

Because subclasses are also considered instances of their parent classes, dog instanceof Animal returns true if Dog extends Animal, even though the check names the parent type rather than the object's exact concrete class.

Example: Inheritance Checks

java
class Animal {}
class Dog extends Animal {}
public class Main {
	public static void main(String[] args) {
		Dog dog = new Dog();
		System.out.println(dog instanceof Animal); // true: Dog extends Animal
	}
}

Checking Null References

A null reference is never an instance of anything, so instanceof on a null variable always evaluates to false regardless of what class you're checking against — this makes instanceof a safe way to check a variable's type without risking a NullPointerException first.

Example: Checking Null References

java
class Animal {}
public class Main {
	public static void main(String[] args) {
		Animal a = null;
		System.out.println(a instanceof Animal); // always false for null
	}
}

Object Class Checks

Since every class in Java implicitly extends Object, checking any non-null object instance against Object with instanceof will always return true — it's rarely useful on its own but illustrates how deep the class hierarchy actually goes.

Example: Object Class Checks

java
class Animal {}
public class Main {
	public static void main(String[] args) {
		Animal a = new Animal();
		System.out.println(a instanceof Object); // true: every class extends Object
	}
}

Interface Checks

instanceof isn't limited to classes — you can also check whether an object's class implements a particular interface, which is common when you need to confirm an object supports a capability (like Comparable) before calling methods that require it.

Example: Interface Checks

java
// Define the interface Flyable
interface Flyable {}
// Define the class Bird
class Bird implements Flyable {}
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		// Create a new Bird object and store it in bird
		Bird bird = new Bird();
		// Print a line to the console
		System.out.println(bird instanceof Flyable);
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Calling instanceof on null, which returns false, so the check can hide a missing object.
  2. Using instanceof with unrelated classes, which is a compile error.
  3. Skipping the instanceof check before a downcast, so a ClassCastException is thrown.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.