Basically, you check if an object is an instance of a specific class. You normally use it, when you have a reference or parameter to an object that is of a super class or interface type and need to know whether the actual object has some other type (normally more concrete).

Example:

public void doSomething(Number param) {
  if( param instanceof Double) {
    System.out.println("param is a Double");
  }
  else if( param instanceof Integer) {
    System.out.println("param is an Integer");
  }

  if( param instanceof Comparable) {
    //subclasses of Number like Double etc. implement Comparable
    //other subclasses might not -> you could pass Number instances that don't implement that interface
    System.out.println("param is comparable"); 
  }
}

Note that if you have to use that operator very often it is generally a hint that your design has some flaws. So in a well designed application you should have to use that operator as little as possible (of course there are exceptions to that general rule).

Answer from Thomas on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › instanceof-keyword-in-java
instanceof Keyword in Java - GeeksforGeeks
July 23, 2025 - // Java program to demonstrate working of instanceof Keyword // Class 1 // Parent class class Parent { } // Class 2 // Child class class Child extends Parent { } // Class 3 // Main class class GFG { // Main driver method public static void main(String[] args) { // Creating object of child class Child cobj = new Child(); // A simple case if (cobj instanceof Child) System.out.println("cobj is instance of Child"); else System.out.println( "cobj is NOT instance of Child"); // instanceof returning true for Parent class also if (cobj instanceof Parent) System.out.println( "cobj is instance of Parent
🌐
DataCamp
datacamp.com › doc › java › instanceof
instanceof Keyword in Java: Usage & Examples
Java keywordsIntroduction To JavaJava File HandlingJava Language BasicsJava ArraysJava Object-Oriented Programming ... The instanceof keyword in Java is a binary operator used to test whether an object is an instance of a specific class or implements a particular interface.
🌐
W3Schools
w3schools.com › java › ref_keyword_instanceof.asp
Java instanceof Keyword
The instanceof keyword compares the instance with type. The return value is either true or false. Read more about objects in our Java Classes/Objects Tutorial.
🌐
Programiz
programiz.com › java-programming › instanceof
Java instanceof (With Examples)
Here, d1 is an instance of Dog class. The instanceof operator checks if d1 is also an instance of the interface Animal. Note: In Java, all the classes are inherited from the Object class.
🌐
Baeldung
baeldung.com › home › java › core java › java instanceof operator
Java instanceof Operator | Baeldung
May 11, 2024 - In Java, every class implicitly inherits from the Object class. Therefore, using the instanceof operator with the Object type will always evaluate to true:
🌐
Medium
medium.com › @medcherrou › understanding-the-instanceof-keyword-in-java-a-complete-guide-7d41e239d63a
Understanding the instanceof Keyword in Java: A Complete Guide | by Med Cherrou | Medium
December 19, 2024 - The instanceof keyword in Java is a powerful tool used to determine whether an object is an instance of a specific class or implements a particular interface.
🌐
Net Informations
net-informations.com › java › cjava › instanceof.htm
What is instanceof keyword in Java?
The "instanceof" keyword in Java serves as a binary operator that allows for the examination of an object's relationship with a given type. It functions by determining whether an object (instance) is a subtype of the specified type, providing a boolean result of either true or false.
Top answer
1 of 16
237

instanceof keyword is a binary operator used to test if an object (instance) is a subtype of a given Type.

Imagine:

interface Domestic {}
class Animal {}
class Dog extends Animal implements Domestic {}
class Cat extends Animal implements Domestic {}

Imagine a dog object, created with Object dog = new Dog(), then:

dog instanceof Domestic // true - Dog implements Domestic
dog instanceof Animal   // true - Dog extends Animal
dog instanceof Dog      // true - Dog is Dog
dog instanceof Object   // true - Object is the parent type of all objects

However, with Object animal = new Animal();,

animal instanceof Dog // false

because Animal is a supertype of Dog and possibly less "refined".

And,

dog instanceof Cat // does not even compile!

This is because Dog is neither a subtype nor a supertype of Cat, and it also does not implement it.

Note that the variable used for dog above is of type Object. This is to show instanceof is a runtime operation and brings us to a/the use case: to react differently based upon an objects type at runtime.

Things to note: expressionThatIsNull instanceof T is false for all Types T.

2 of 16
44

It's an operator that returns true if the left side of the expression is an instance of the class name on the right side.

Think about it this way. Say all the houses on your block were built from the same blueprints. Ten houses (objects), one set of blueprints (class definition).

instanceof is a useful tool when you've got a collection of objects and you're not sure what they are. Let's say you've got a collection of controls on a form. You want to read the checked state of whatever checkboxes are there, but you can't ask a plain old object for its checked state. Instead, you'd see if each object is a checkbox, and if it is, cast it to a checkbox and check its properties.

if (obj instanceof Checkbox)
{
    Checkbox cb = (Checkbox)obj;
    boolean state = cb.getState();
}
Find elsewhere
🌐
Tutorialspoint
tutorialspoint.com › java › instanceof_keyword_in_java.htm
Java - instanceof Keyword
Java instanceof keyword is an operator which is used only for object reference variables. This operator checks whether a Java object is of a particular type (class type or interface type).
🌐
Javatpoint
javatpoint.com › downcasting-with-instanceof-operator
Java instanceof - javatpoint
The Java instanceof operator is used to test if the object or instance is an instanceof the specified type (class or subclass or interface), how downcasting in java is possible with instanceof operator:
🌐
Study.com
study.com › business courses › business 104: information systems and computer applications
How to Use InstanceOf Operator in Java - Lesson | Study.com
March 19, 2019 - The Java instanceof keyword is used to check if an object is a certain type. It returns true or false. For example, we can check if a variable is a type of String; we can test classes to see if they are certain types (e.g., is a Birch a Tree ...
🌐
Simplilearn
simplilearn.com › home › resources › software development › understanding the use of instanceof in java
Understanding the Use of Instanceof in Java with Examples
September 14, 2025 - Instanceof in java is a binary operator used to check if the specified object is an instance of a class, subclass, or interface. Learn all about it now!
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Wikibooks
en.wikibooks.org › wiki › Java_Programming › Keywords › instanceof
Java Programming/Keywords/instanceof - Wikibooks, open books for an open world
The <object-reference> instanceof Object will return true for all non-null object references, since all Java objects are inherited from Object.
🌐
YouTube
youtube.com › watch
instanceof Java Keyword Tutorial #90 - YouTube
$1,500 OFF ANY Springboard Tech Bootcamps with my code ALEXLEE1500. See if you qualify for the JOB GUARANTEE! 👉 https://bit.ly/3HX970hThe instanceof keyword...
Published   July 2, 2020
🌐
Lachimi
lachimi.com › java-keyword-instanceof
lachimi: The instanceof Keyword in Java
The `instanceof` keyword in Java is a powerful operator used to test whether an object is an instance of a specific class or subclass.
🌐
Armedia
armedia.com › home › blog › “instanceof”, why and how to avoid it in code
Java “instanceOf”: Why And How To Avoid It In Code
November 23, 2019 - The java “instanceof” operator is used to test whether the object is an instance of the specified type (class or subclass or interface). It is also known as type comparison operator because it compares the instance with type.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › language › pattern-matching-instanceof.html
Pattern Matching for instanceof - Java
October 20, 2025 - Note:Removing this conversion step also makes your code safer. Testing an object's type with the instanceof, then assigning that object to a new variable with a cast can introduce coding errors in your application.
🌐
BeginnersBook -
beginnersbook.com › home › java › java instanceof with examples
Java instanceof With Examples
October 30, 2022 - The instanceof keyword is an operator in java. It checks if the given object is an instance of a specified class or interface.
🌐
TechVidvan
techvidvan.com › tutorials › java-instanceof
instanceof Keyword in Java - TechVidvan
December 28, 2023 - The instanceof operator is utilized to verify if an object belongs to a specific class or adheres to a certain interface. This enables you to confirm the object’s type before carrying out any actions specific to that type. The instanceof operator in Java provides a true or false outcome, ...