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
🌐
DataCamp
datacamp.com › doc › java › instanceof
instanceof Keyword in Java: Usage & Examples
String str = null; boolean result = str instanceof String; // result is false
🌐
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 ...
🌐
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
🌐
GeeksforGeeks
geeksforgeeks.org › java › instanceof-keyword-in-java
instanceof Keyword in Java - GeeksforGeeks
July 23, 2025 - // Java Program to Illustrate ... classes import java.io.*; // Main class class GFG { public static void main(String[] args) { // Creating object of class inside main() GFG object = new GFG(); // Returning instanceof System.out.println(object instanceof GFG); } }...
🌐
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.
🌐
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...
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › language › pattern-matching-instanceof.html
Pattern Matching for the instanceof Operator
January 16, 2025 - 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. You might change the type of one of the objects (either the tested object or the new variable) and accidentally forget to change the type of the other object. A pattern is a combination of a test, which is called a predicate; a target; and a set of local variables, which are called pattern variables. The getPerimeter example contains two patterns, s instanceof Rectangle r and s instanceof Circle c:
🌐
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.
🌐
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.
🌐
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.
🌐
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.
🌐
Baeldung
baeldung.com › home › java › core java › pattern matching for instanceof in java
Pattern Matching for instanceof in Java | Baeldung
January 16, 2024 - At some point, we’ve probably all written or seen code that includes some kind of conditional logic to test if an object has a specific type. Typically, we might do this with the instanceof operator followed by a cast.
🌐
Edureka
edureka.co › blog › instanceof-in-java
InstanceOf In Java | Java InstanceOf Operator | Edureka
June 19, 2023 - Java Tutorial For Beginners – Java Programming Made Easy! ... InstanceOf In Java is an operator, which is used to check the type of an object. In other terms, it tests whether the object is an instance of a specific class or an interface.
🌐
BeginnersBook
beginnersbook.com › 2022 › 10 › java-instanceof-with-examples
Java instanceof With Examples
October 30, 2022 - interface MyInterface{ } public class JavaExample implements MyInterface{ public static void main(String[] args) { JavaExample obj = new JavaExample(); //comparing subclass object with parent interface System.out.println(obj instanceof MyInterface); } }