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
🌐
Baeldung
baeldung.com › home › java › core java › java instanceof operator
Java instanceof Operator | Baeldung
May 11, 2024 - Technically speaking, we’re only allowed to use instanceof along with reified types in Java. A type is reified if its type information is present at runtime. ... One great feature that Java 8 has brought us is the Stream API. We often convert a type A collection to a type B collection using Stream‘s map() method. If the type conversion is done by typecasting, we may want to check the types before performing the typecasting to avoid the ClassCastException. Next, let’s see an example.
🌐
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
🌐
Programiz
programiz.com › java-programming › instanceof
Java instanceof (With Examples)
In the above example, the Dog class implements the Animal interface. Inside the print statement, notice the expression, ... 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 ...
🌐
DataCamp
datacamp.com › doc › java › instanceof
instanceof Keyword in Java: Usage & Examples
String str = null; boolean result = str instanceof String; // result is false
🌐
GeeksforGeeks
geeksforgeeks.org › java › instanceof-keyword-in-java
instanceof Keyword in Java - GeeksforGeeks
July 23, 2025 - ... // Java program to demonstrate ... { int value = 10; } // Driver class class Test { public static void main(String[] args) { Parent cobj = new Child(); Parent par = cobj; // Using instanceof to make sure that par // is a valid ...
🌐
W3Schools
w3schools.com › java › ref_keyword_instanceof.asp
Java instanceof Keyword
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods
🌐
Jenkov
jenkov.com › tutorials › java › instanceof.html
Java instanceof operator
July 4, 2020 - Map<Object, Object> map = new HashMap(); boolean mapIsObject = map instanceof HashMap; Notice that the type of the variable is now Map and not HashMap . Map is not a subclass of HashMap.
Find elsewhere
🌐
How to do in Java
howtodoinjava.com › home › java object oriented programming › java instanceof operator
Java instanceof Operator - HowToDoInJava
January 3, 2023 - The instanceof operator helps in avoiding ClassCastException in runtime. Consider the following example where we are trying to typecast a list to LinkedList class, where the original variable is of type ArrayList.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › language › pattern-matching-instanceof.html
Pattern Matching for instanceof - Java
October 20, 2025 - 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. The target is the argument of the predicate, which is the Shape value. The pattern variables are those that store data from the target only if the predicate returns true, which are the variables r and s. A type pattern consists of a predicate that specifies a type, along with a single pattern variable. In this example, the type patterns are Rectangle r and Circle c.
🌐
IONOS
ionos.com › digital guide › websites › web development › java instanceof operator
How the Java instanceof operator works
October 22, 2024 - You can use the Java operator instanceof to check if an object belongs to a specific class. The operator provides the answer in the form of a Boolean statement, i.e. “true” or “false”. Checking whether the object and class match be­fore­hand can help you to avoid error messages later on. The nature of some variables in Java is not always im­me­di­ate­ly apparent. In large Java projects with numerous variables, input data, for example...
🌐
Medium
medium.com › @AlexanderObregon › how-javas-instanceof-operator-works-09071a27cd3b
How Java’s instanceof Operator Works | Medium
February 28, 2025 - When instanceof is used on a child class instance with a parent class reference, it returns true because the object belongs to both types. This is possible because Java enforces a strict parent-child relationship where every subclass inherits from a single superclass.
🌐
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, but the good design would avoid using this keyword.
🌐
Scaler
scaler.com › home › topics › what is instanceof java operator?
What is Instanceof Java Operator? - Scaler Topics
January 3, 2023 - In this example, we are going to see how to use instanceof in Java to check if one object belongs to a particular type or not.
🌐
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 - 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 or a BoysName?).
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();
}
🌐
Blogger
javarevisited.blogspot.com › 2015 › 12 › 10-points-about-instanceof-operator-in-java-example.html
How to use instanceof operator in Java with example
August 19, 2021 - This operator has a form of object instanceof Type and returns true if the object satisfies IS-A relationship with the Type i.e. object is an instance of class Type or object is the instance of a class which extends Type or object is an instance of a class which implements interface Type. Once an object passed the instanceof check, it's safe to type-cast into that type, without worrying of java.lang.ClassCastException.
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-instanceof-with-examples
Java instanceof With Examples
October 30, 2022 - When we compared the object of ... true. class MyClass{ } public class JavaExample extends MyClass{ public static void main(String[] args) { JavaExample obj = new JavaExample(); //checking subclass object for superclass type System.out.println(obj instanceof MyClass); } }...