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 - In Java, instanceof is a keyword used for checking if a reference variable contains a given type of object reference or not. Following is a Java program to show different behaviors of instanceof.
🌐
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:
🌐
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.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › language › pattern-matching-instanceof.html
Pattern Matching for instanceof - Java
October 20, 2025 - A pattern is a combination of a predicate, or test, that can be applied to a target and a set of local variables, called pattern variables, that are assigned values extracted from the target only if the test is successful. The predicate is a Boolean-valued function of one argument; in this case, it’s the instanceof operator testing whether the Shape argument is a Rectangle or a Circle.
🌐
Programiz
programiz.com › java-programming › instanceof
Java instanceof (With Examples)
Become a certified Java programmer. Try Programiz PRO! ... The instanceof operator in Java is used to check whether an object is an instance of a particular class or not.
🌐
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.
🌐
Armedia
armedia.com › home › blog › “instanceof”, why and how to avoid it in code
Java “instanceOf”: Why And How To Avoid It In Code - Armedia
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.
Find elsewhere
🌐
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:
🌐
IONOS
ionos.com › digital guide › websites › web development › java instanceof operator
How the Java instanceof operator works
October 22, 2024 - When you use instanceof in Java, the operator compares a reference variable with a specific class that is also specified by the user. It doesn’t include any ad­di­tion­al in­for­ma­tion about the nature of the object or the class.
🌐
Medium
medium.com › @AlexanderObregon › how-javas-instanceof-operator-works-09071a27cd3b
How Java’s instanceof Operator Works | Medium
February 28, 2025 - The instanceof operator does not compare the declared type of a reference; it inspects the actual object’s class at runtime. Under the hood, the Java compiler translates instanceof into a dedicated bytecode instruction (instanceof).
🌐
Jenkov
jenkov.com › tutorials › java › instanceof.html
Java instanceof operator
The Java instanceof operator can determine whether a given object is an instance of a given class or interface. The Java instanceof operator was updated in Java 14 with pattern matching functionality, making it more concise to use.
🌐
OpenJDK
openjdk.org › jeps › 394
JEP 394: Pattern Matching for instanceof
July 27, 2020 - Make it a compile-time error for a pattern instanceof expression to compare an expression of type S against a pattern of type T, where S is a subtype of T. (This instanceof expression will always succeed and is then pointless. The opposite case, where a pattern match will always fail, is already a compile-time error.) Other refinements may be incorporated based on further feedback. Nearly every program includes some sort of logic that combines testing if an expression has a certain type or structure, and then conditionally extracting components of its state for further processing. For example, all Java programmers are familiar with the instanceof-and-cast idiom:
🌐
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
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();
}
🌐
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.
🌐
Oracle
docs.oracle.com › cd › E13155_01 › wlp › docs103 › javadoc › com › bea › p13n › expression › operator › Instanceof.html
Instanceof (Oracle WebLogic Portal 10.3 JavaDoc: Overview)
Implementation of the Instanceof operator. Returns a Boolean if the Object parameter (which can be an expression) is an instance of a class type. Input 1: An object or Expression returning an object.