HOWTO · Java

Java Not InstanceOf

This tutorial demonstrates how to negate the instanceof keyword in Java, providing clear examples and explanations. Learn to control your code flow effectively by checking if an object is not an instance of a specific class or interface. Enhance your Java programming skills with practical applications and insights.

On this page

In Java, there is no separate “not instanceof” operator. Write !(object instanceof Type) to negate the entire type check. This guide explains what that expression means, how inheritance and null affect it, and how to use it while filtering objects.

What instanceof Checks in Java

The instanceof operator evaluates whether a non-null object can be treated as a specified reference type: a class, superclass, or interface that its runtime class is compatible with. For example, an object created from a subclass also matches checks for its superclasses and implemented interfaces. The expression produces a boolean value, so it can be used in an if condition or combined with other boolean expressions.

The left side must be a reference expression and the type on the right must be compatible with it. Java rejects some checks when the types cannot possibly be related. instanceof does not convert the object; it only answers whether the type relationship holds. A successful check can be useful before a cast, but the cast itself is a separate operation.

How to Negate an instanceof Check

Put the complete type check in parentheses after the logical NOT operator: !(object instanceof Type). The parentheses make clear that ! reverses the boolean result of the whole instanceof expression. When the object matches Type, the inner check is true and the negated condition is false. When it does not match, the inner check is false and the negated condition is true.

You may also see object instanceof Type == false, but !(object instanceof Type) expresses the same intent more directly and is easier to scan. Use the negated form in an if statement when the nonmatching case is the branch you need to handle.

Example: Filter Objects by Type

This complete example keeps animals that are not cats and processes them. The check happens for each element before the code uses that element in the non-cat branch.

Run this example:

import java.util.List;

public class Main {
    interface Animal {}
    static class Dog implements Animal {}
    static class Cat implements Animal {}

    public static void main(String[] args) {
        List<Animal> animals = List.of(new Dog(), new Cat(), new Dog());
        for (Animal animal : animals) {
            if (!(animal instanceof Cat)) {
                System.out.println("Processing " + animal.getClass().getSimpleName());
            }
        }
    }
}
Processing Dog
Processing Dog

How Inheritance and null Affect the Result

A subclass instance matches instanceof checks for its superclass and implemented interfaces as well as its own class. Consequently, negating a check for a parent type is false when the object is an instance of one of its child classes. Choose the type in the condition carefully: checking against a broad parent type excludes more objects than checking against a specific child type.

The expression null instanceof Type is always false. Therefore, !(value instanceof Type) is true when value is null. If the later branch assumes a non-null object or calls a method on it, account for that separately; a negated type check alone does not establish that the value is non-null.

When to Use a Negated instanceof Check

Use !(value instanceof Type) when the nonmatching case naturally describes the work, such as skipping one kind of item while processing a mixed collection. Keep the condition close to the branch it controls and use a specific type that reflects the intended exclusion. If both matching and nonmatching cases need substantial logic, explicit branches may be clearer.

A negative check does not make an object safe to cast to some other type. Before casting or calling type-specific methods, verify the positive type assumption that those operations require. Also consider the null behavior when the value may be absent. These checks make the condition’s meaning and the assumptions in the following code easier to review.