Instanceof Operator

Su Suriya Ravichandran Updated 14 Nov 2026
7 min read ·Lesson 58 of 306

The instanceof operator tests, at runtime, whether an object is an instance of a given type: a class, a subclass of it, or an interface it implements. It returns true or false. Since Java 16, instanceof can also bind a variable of the tested type in the same step, which removes the need for a separate cast. It is the standard way to safely work with an object whose exact type is not known at compile time.

Basic Syntax

object instanceof Type
Object value = "hello";

System.out.println(value instanceof String);     // true
System.out.println(value instanceof Object);     // true: every object is an Object
System.out.println(value instanceof Integer);    // false
System.out.println(value instanceof CharSequence); // true: String implements CharSequence

The test is based on the object's actual runtime class, not on the declared type of the variable. value is declared as Object, but the object it refers to is a String.

instanceof and Inheritance

instanceof is true for the object's own class and for every supertype above it:

class Vehicle { }
class Car extends Vehicle { }
class ElectricCar extends Car { }

Vehicle v = new ElectricCar();

System.out.println(v instanceof ElectricCar);   // true
System.out.println(v instanceof Car);           // true
System.out.println(v instanceof Vehicle);       // true
System.out.println(v instanceof Object);        // true

An ElectricCar is a Car and is a Vehicle, so all of these hold. The test answers "can this object be treated as this type?", not "is its class exactly this type?". To check the exact class, compare getClass():

System.out.println(v.getClass() == Car.class);          // false
System.out.println(v.getClass() == ElectricCar.class);  // true

null Is Never an Instance

instanceof always returns false for null, without throwing:

String s = null;
System.out.println(s instanceof String);   // false

This makes instanceof a built-in null check. if (x instanceof Foo) never needs a separate x != null test.

Compile-Time Check

The compiler rejects an instanceof test that can never be true:

String text = "abc";
System.out.println(text instanceof Integer);   // compile error: incompatible types

A String variable can only ever hold a String (a final class), and String and Integer are unrelated, so the test is pointless and the compiler reports it. Tests against interfaces are usually allowed, because some subclass might implement the interface, unless the declared class is final and does not implement it.

Using instanceof Before a Cast

instanceof exists mainly to make downcasting safe. A cast to the wrong type throws ClassCastException at runtime:

Object o = 42;
String s = (String) o;     // ClassCastException: Integer cannot be cast to String

The traditional pattern checks first, then casts:

if (o instanceof String) {
    String s = (String) o;
    System.out.println(s.length());
}

The type name appears twice, and a copy-paste mistake could test one type and cast to another.

Pattern Matching for instanceof (Java 16+)

Modern Java combines the test, the cast, and a new variable in one expression:

if (o instanceof String s) {
    System.out.println(s.length());    // s is already a String
}

String s is a type pattern. If the test succeeds, s is declared and assigned the object, already typed as String. If the test fails, s does not exist.

Where the pattern variable is in scope

The variable is available wherever the compiler can prove the test succeeded:

if (o instanceof String s && s.length() > 3) {      // usable after &&
    System.out.println(s.toUpperCase());
}

if (!(o instanceof String s)) {
    return;                                         // leave if not a String
}
System.out.println(s.strip());                      // usable here: we only get here if it matched

The second form, an early return when the test fails, is common for guarding the rest of a method.

It is not available where the test may have failed:

if (o instanceof String s || s.isEmpty()) { }   // compile error: s may not be assigned

With ||, the right side runs exactly when the left side is false, so s would not exist there.

In equals methods

Pattern matching makes the standard equals implementation shorter and safer:

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Point other)) return false;
    return x == other.x && y == other.y;
}

The instanceof check also handles null, since null instanceof Point is false.

Record Patterns (Java 21+)

For records, a pattern can also take the object apart into its components:

record Point(int x, int y) { }

Object obj = new Point(3, 4);

if (obj instanceof Point(int x, int y)) {
    System.out.println("x=" + x + ", y=" + y);    // x=3, y=4
}

Point(int x, int y) matches a Point and extracts its components into new variables, calling the record's accessors behind the scenes. Record patterns can nest:

record Line(Point start, Point end) { }

if (obj instanceof Line(Point(var x1, var y1), Point(var x2, var y2))) {
    double length = Math.hypot(x2 - x1, y2 - y1);
}

var lets the compiler infer each component's type.

instanceof in switch (Java 21+)

When testing one object against several types, a pattern-matching switch is clearer than a chain of instanceof checks:

static String describe(Object o) {
    if (o instanceof Integer i)      return "int " + i;
    else if (o instanceof String s)  return "text of length " + s.length();
    else if (o instanceof int[] arr) return "array of " + arr.length;
    else                             return "other";
}

static String describeSwitch(Object o) {
    return switch (o) {
        case Integer i  -> "int " + i;
        case String s   -> "text of length " + s.length();
        case int[] arr  -> "array of " + arr.length;
        case null       -> "null";
        default         -> "other";
    };
}

Both work. The switch version reads as a table, can include when guards, and, for sealed hierarchies, lets the compiler verify that every subtype is handled.

When to Use instanceof, and When Not To

instanceof is the right tool when:

  • Implementing equals(Object).
  • Handling values from an untyped source: deserialized data, Object parameters in older APIs, heterogeneous collections, reflection.
  • Deconstructing data types such as records, especially in a sealed hierarchy.
  • Checking for an optional capability: if (resource instanceof AutoCloseable c) c.close();

It is often a warning sign when it is used to decide behavior based on type:

// Fragile: every new shape requires editing this method
double area(Shape s) {
    if (s instanceof Circle c)    return Math.PI * c.r() * c.r();
    if (s instanceof Square q)    return q.side() * q.side();
    throw new IllegalArgumentException("unknown shape");
}

In a class hierarchy designed for extension, put the behavior in the classes with an overridden method (s.area()), and let polymorphism choose. With sealed types and pattern-matching switch, the type-switching style becomes safe, because the compiler reports any missing case. The choice depends on whether new types or new operations are more likely to be added.

Practical Example: Processing Mixed Events

import java.util.List;

public class EventLog {
    sealed interface Event permits Login, Purchase, Error { }
    record Login(String user) implements Event { }
    record Purchase(String user, double amount) implements Event { }
    record Error(String code, String message) implements Event { }

    static double revenue(List<Object> rawEvents) {
        double total = 0;
        for (Object raw : rawEvents) {
            if (!(raw instanceof Event e)) {
                System.out.println("Skipping unknown item: " + raw);
                continue;
            }
            if (e instanceof Purchase(String user, double amount) && amount > 0) {
                total += amount;
            }
        }
        return total;
    }

    static String summarize(Event e) {
        return switch (e) {
            case Login l -> l.user() + " logged in";
            case Purchase(String user, double amount) when amount >= 10_000 ->
                    user + " made a LARGE purchase of " + amount;
            case Purchase p -> p.user() + " bought " + p.amount();
            case Error err -> "ERROR " + err.code() + ": " + err.message();
        };
    }

    public static void main(String[] args) {
        List<Object> raw = List.of(
            new Login("ada"),
            new Purchase("ada", 2499),
            "corrupted-line",
            new Purchase("linus", 15000),
            new Error("E42", "Payment gateway timeout")
        );

        System.out.println("Revenue: " + revenue(raw));

        for (Object o : raw) {
            if (o instanceof Event e) {
                System.out.println(summarize(e));
            }
        }
    }
}

Output:

Skipping unknown item: corrupted-line
Revenue: 17499.0
ada logged in
ada bought 2499.0
linus made a LARGE purchase of 15000.0
ERROR E42: Payment gateway timeout

The input list holds Object values, as data read from a queue or file often does. raw instanceof Event e filters out anything that is not an event and binds the rest to a typed variable; because instanceof is false for null, nulls would be skipped too. A record pattern extracts amount from purchases in a single condition. In summarize, the switch needs no default: Event is sealed, so the compiler knows the three possible types and would reject the switch if one were missing. The guarded Purchase case must come before the unguarded one, since cases are checked from top to bottom.

0 Comments

Reviewed before they appear

No comments yet.

Java
Java Introduction Java Features Java Syntax Java Comments Java Statements Java Output Java Variables Java Data Types Java Type Casting Java Operators Java Strings Java Numbers Java Math Java Booleans Java User Input Java Conditions Java If Else Java Switch Java While Loop Java For Loop Java Break Java Continue Java Arrays Java Methods Java Parameters Java Return Values Java Scope Java Recursion Install Java Install JDK Java IDEs Java JDK Java JRE Java JVM Java Compiler Java File Compile Java Run Java JShell Basics Java Packages Java OOP Java Classes Java Objects Class Methods Constructors Constructor Overload this Keyword static Keyword final Keyword Encapsulation Inheritance Polymorphism Abstraction Access Modifiers super Keyword Method Overload Method Override Instanceof Operator Nested Classes Inner Classes Anonymous Classes Enums Records Array Basics Array Length Loop Arrays Multidimensional Arrays Array Copy Array Sorting Array Search ArrayList LinkedList HashSet TreeSet HashMap TreeMap Queue Interface Deque Interface Stack Class Iterator List Interface Set Interface Map Interface Collections Class Comparable Comparator Generics Basics Generic Classes Generic Methods Wildcards String Basics String Length String Methods String Concatenation String Compare String Search String Replace String Split String Format StringBuilder StringBuffer Text Blocks Regular Expressions Number Types Integer Class Float Class Double Class BigInteger BigDecimal Math Class Random Numbers Number Format Date Basics LocalDate LocalTime LocalDateTime ZonedDateTime Date Formatting Date Parsing Period Class Duration Class Java Instant Class Java Exception Basics Java try...catch Java Multiple Catch Blocks Java finally Block Java throw Keyword Java throws Keyword Java Checked Exceptions Java Unchecked Exceptions Java Custom Exceptions Java try-with-resources Java Exception Chaining Java File Basics Java Create Files Java Read Files Java Write Files Java Delete Files Java File Paths Java File Class Java BufferedReader Java BufferedWriter Java FileInputStream Java FileOutputStream Java Object Streams Java Directory Handling Java NIO Basics Java Files API Java Input Java Scanner Class Java Console Input Java PrintStream Java InputStream Java OutputStream Java Reader Class Java Writer Class Java Buffered Streams Java Data Streams Java Generics Introduction Java Generic Types Java Generic Methods Java Bounded Types Java Wildcards Java Type Erasure Java Generic Interfaces Java Generic Collections Java Lambda Basics Java Lambda Syntax Java Functional Interfaces Java Method References Java Stream API Java Stream Creation Java Stream Filter Java Stream Map Java Stream Reduce Java Stream Sort Java Stream Collect Java Collectors Class Java Optional Class Java Parallel Streams Java Threads: Thread Basics Create a Thread in Java Java Thread Class Java Runnable Interface Java Thread States Java Thread Sleep Java Thread Join Java Thread Priority Java Synchronization Java Thread Safety Java Race Conditions Java Deadlocks Java Executor Service Java Thread Pools Java Callable Interface Java Future Interface Java CompletableFuture Java Concurrent Collections Java Atomic Classes Java Virtual Threads Java Networking Basics Java URL Class Java URI Class Java HttpClient Java HTTP Requests Java HTTP Responses Java Socket Basics Java ServerSocket Java DatagramSocket Java WebSocket Basics Java JDBC Basics Java JDBC Setup Java Database Connection Java JDBC Connection Interface Java JDBC Statement Java PreparedStatement Java ResultSet Java JDBC CRUD Operations Java JDBC Transactions Java JDBC Batch Update Java Connection Pooling with HikariCP Java JPA Basics: Entities, EntityManager and JPQL Java Hibernate Basics: SessionFactory, Session and HQL Package in Java: Basics Explained with Examples Create Package in Java: Step-by-Step Guide with Examples Import Package in Java: The import Statement Explained Access Control in Java: Packages, Classes and Members JAR File in Java: Create, Run and Inspect JAR Files Java Modules: Introduction to the Java Module System module-info in Java: Every Module Directive Explained Java Module Path: How --module-path Finds Your Modules Java Classpath: How -cp and CLASSPATH Find Your Classes Java Annotations: Basics of @ Metadata in Java Java Built-in Annotations: @Override, @Deprecated and More Java Custom Annotations: Create Your Own @interface Java Annotation Targets: @Target and ElementType Explained Java Reflection API: Inspect and Use Classes at Runtime Java Class Metadata: Names, Modifiers, Hierarchy and Generic Java Dynamic Objects: Create Objects Dynamically With Reflec Java Dynamic Proxy: Intercept Method Calls With Proxy and In Java Security Java Secure Coding Java Password Hashing Java Input Validation Java Cryptography Basics Java KeyStore Basics Java TLS Basics Java SecureRandom Java JUnit Basics Java JUnit Test Methods JUnit Assertions JUnit Test Fixtures JUnit Parameterized Tests Mockito in Java Java Mock Objects Java Integration Testing Java Code Coverage Maven in Java Create a Maven Project in Java Maven Dependencies Maven Plugins Gradle in Java Gradle Build Gradle Dependencies Build Executable JAR in Java Build WAR File in Java HTTP Basics in Java Java Servlet Basics Servlet Lifecycle in Java HttpServletRequest in Java HttpServletResponse in Java JSP in Java JSTL in JSP REST API in Java JSON in Java Java Web Application Spring Framework Basics Spring Boot Tutorial Spring MVC Spring Data JPA Spring Security Spring Boot REST API Jakarta EE Basics Java Deployment JAR Deployment Java WAR Deployment Run Java as a Service Docker Java: Dockerize a Java Application Java Environment Variables and Configuration Java Logging Basics Java Production Setup Java JVM Options Java JVM Monitoring Java Hello World App Java Calculator App Java Number Guess Game Java Student Manager Java Library System Java Bank App Java Todo App Java File Manager Java Quiz App Java REST API Project Java Chat Server Java Employee Manager Java E-commerce API
Ask about this post
AI Ask about this post

Ask questions about Instanceof Operator and get answers drawn from it.

Signed-in readers only.