eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Introduction

In this quick tutorial, we’re going to take a look at the difference between instanceof, Class.isInstance, and Class.isAssignableFrom. We’ll learn how to use each method and what the differences are between them.

2. Setup

Let’s set up an interface and a couple of classes to use while we explore the instanceof, Class.isInstance, and Class.isAssignableFrom functionality.

First, let’s define an interface:

public interface Shape {
}

Next, let’s define a class that implements Shape:

public class Triangle implements Shape {
}

Now, we’ll create a class that extends Triangle:

public class IsoscelesTriangle extends Triangle {
}

3. instanceof

The instanceof keyword is a binary operator, and we can use it to verify whether a certain object is an instance of a given type. Therefore, the result of the operation is either true or false. Additionally, the instanceof keyword is the most common and straightforward way to check if an object subtypes another type.

Let’s use our classes with the instanceof operator:

Shape shape = new Triangle();
Triangle triangle = new Triangle();
IsoscelesTriangle isoscelesTriangle = new IsoscelesTriangle();
Shape nonspecificShape = null;

assertTrue(shape instanceof Shape);
assertTrue(triangle instanceof Shape);
assertTrue(isoscelesTriangle instanceof Shape);
assertFalse(nonspecificShape instanceof Shape);

assertTrue(shape instanceof Triangle);
assertTrue(triangle instanceof Triangle);
assertTrue(isoscelesTriangle instanceof Triangle);
assertFalse(nonspecificShape instanceof Triangle);

assertFalse(shape instanceof IsoscelesTriangle);
assertFalse(triangle instanceof IsoscelesTriangle);
assertTrue(isoscelesTriangle instanceof IsoscelesTriangle);
assertFalse(nonspecificShape instanceof IsoscelesTriangle);

With the above code snippet, we can see that the right-hand side type is more generic than the left-hand side object. More specifically, the instanceof operator will process null values to false.

4. Class.isInstance

The isInstance method on the Class class is equivalent to the instanceof operator. The isInstance method was introduced in Java 1.1 because it can be used dynamically. Generally, this method will return true if the argument isn’t null and can be successfully cast to the reference type without raising a ClassCastException.

Let’s look at how we can use the isInstance method with the interface and classes we defined:

Shape shape = new Triangle();
Triangle triangle = new Triangle();
IsoscelesTriangle isoscelesTriangle = new IsoscelesTriangle();
Triangle isoscelesTriangle2 = new IsoscelesTriangle();
Shape nonspecificShape = null;

assertTrue(Shape.class.isInstance(shape));
assertTrue(Shape.class.isInstance(triangle));
assertTrue(Shape.class.isInstance(isoscelesTriangle));
assertTrue(Shape.class.isInstance(isoscelesTriangle2));
assertFalse(Shape.class.isInstance(nonspecificShape));

assertTrue(Triangle.class.isInstance(shape));
assertTrue(Triangle.class.isInstance(triangle));
assertTrue(Triangle.class.isInstance(isoscelesTriangle));
assertTrue(Triangle.class.isInstance(isoscelesTriangle2));

assertFalse(IsoscelesTriangle.class.isInstance(shape));
assertFalse(IsoscelesTriangle.class.isInstance(triangle));
assertTrue(IsoscelesTriangle.class.isInstance(isoscelesTriangle));
assertTrue(IsoscelesTriangle.class.isInstance(isoscelesTriangle2));

As we can see, the right-hand side can be equivalent to or more specific than the left-hand side. In particular, providing null to the isInstance method returns false.

5. Class.isAssignableFrom

The Class.isAssignableFrom method will return true if the Class on the left-hand side of the statement is the same as or is a superclass or superinterface of the provided Class parameter.

Let’s use our classes with the isAssignableFrom method:

Shape shape = new Triangle();
Triangle triangle = new Triangle();
IsoscelesTriangle isoscelesTriangle = new IsoscelesTriangle();
Triangle isoscelesTriangle2 = new IsoscelesTriangle();

assertFalse(shape.getClass().isAssignableFrom(Shape.class));
assertTrue(shape.getClass().isAssignableFrom(shape.getClass()));
assertTrue(shape.getClass().isAssignableFrom(triangle.getClass()));
assertTrue(shape.getClass().isAssignableFrom(isoscelesTriangle.getClass()));
assertTrue(shape.getClass().isAssignableFrom(isoscelesTriangle2.getClass()));

assertFalse(triangle.getClass().isAssignableFrom(Shape.class));
assertTrue(triangle.getClass().isAssignableFrom(shape.getClass()));
assertTrue(triangle.getClass().isAssignableFrom(triangle.getClass()));
assertTrue(triangle.getClass().isAssignableFrom(isoscelesTriangle.getClass()));
assertTrue(triangle.getClass().isAssignableFrom(isoscelesTriangle2.getClass()));

assertFalse(isoscelesTriangle.getClass().isAssignableFrom(Shape.class));
assertFalse(isoscelesTriangle.getClass().isAssignableFrom(shape.getClass()));
assertFalse(isoscelesTriangle.getClass().isAssignableFrom(triangle.getClass()));
assertTrue(isoscelesTriangle.getClass().isAssignableFrom(isoscelesTriangle.getClass()));
assertTrue(isoscelesTriangle.getClass().isAssignableFrom(isoscelesTriangle2.getClass()));

assertFalse(isoscelesTriangle2.getClass().isAssignableFrom(Shape.class));
assertFalse(isoscelesTriangle2.getClass().isAssignableFrom(shape.getClass()));
assertFalse(isoscelesTriangle2.getClass().isAssignableFrom(triangle.getClass()));
assertTrue(isoscelesTriangle2.getClass().isAssignableFrom(isoscelesTriangle.getClass()));
assertTrue(isoscelesTriangle2.getClass().isAssignableFrom(isoscelesTriangle2.getClass()));

As with the isInstance example, we can clearly see that the right-hand side must be either the same or more specific than the left-hand side. We can also see that we’re never able to assign our Shape interface.

6. The Differences

Now that we’ve laid out a few detailed examples, let’s go over some of the differences.

6.1. Semantical Differences

Superficially, instanceof is a Java Language keyword. In contrast, both isInstance and isAssignableFrom are native methods from Class type.

Semantically, we use them to verify the different relationships between two programming elements:

  • Two objects: we can test if the two objects are identical or equal.
  • An object and a type: we can check whether the object is an instance of the type. Obviously, both the instanceof keyword and the isInstance method belong to this category.
  • Two types: we can examine whether one type is compatible with another type, such as the isAssignableFrom method.

6.2. Usage Corner Case Differences

Firstly, they differ with a null value:

assertFalse(null instanceof Shape);
assertFalse(Shape.class.isInstance(null));
assertFalse(Shape.class.isAssignableFrom(null)); // NullPointerException

From the code snippet above, both instanceof and isInstance return false; however, the isAssignableFrom method throws NullPointerException.

Secondly, they differ with primitive types:

assertFalse(10 instanceof int); // illegal
assertFalse(int.class.isInstance(10));
assertTrue(Integer.class.isInstance(10));
assertTrue(int.class.isAssignableFrom(int.class));
assertFalse(float.class.isAssignableFrom(int.class));

As we can see, the instanceof keyword doesn’t support primitive types. If we use the isInstance method with an int value, the Java compiler will transform the int value into an Integer object. So, the isInstance method supports primitive types but always returns false. When we use the isAssignableFrom method, the result depends on the exact type values.

Thirdly, they differ with class instance variables:

Shape shape = new Triangle();
Triangle triangle = new Triangle();
Class<?> clazz = shape.getClass();

assertFalse(triangle instanceof clazz); // illegal
assertTrue(clazz.isInstance(triangle));
assertTrue(clazz.isAssignableFrom(triangle.getClass()));

From the code snippet above, we realize that both isInstance and isAssignableFrom methods support the class instance variables, but the instanceof keyword doesn’t.

6.3. Bytecode Differences

In a compiled class file, they utilize different opcodes:

  • The instanceof keyword corresponds to the instanceof opcode
  • Both the isInstance and isAssignableFrom methods will use the invokevirtual opcode

In the JVM Instruction Set, the instanceof opcode has a value of 193, and it has a two-byte operand:

instanceof
indexbyte1
indexbyte2

Then, the JVM will compute (indexbyte1 << 8) | indexbyte2 into an index. And this index points to the run-time constant pool of the current class. At the index, the run-time constant pool contains a symbolic reference to a CONSTANT_Class_info constant. And, this constant is exactly the value that the right-hand side of the instanceof keyword needs.

Furthermore, it also explains why the instanceof keyword can’t use the class instance variable. This is because the instanceof opcode needs a constant type in the run-time constant pool, and we can’t replace a constant type with a class instance variable.

And, where is the left-hand side object information of the instanceof keyword stored? It’s on the top of the operand stack. So, the instanceof keyword requires an object on the operand stack and a constant type in the run-time constant pool.

In the JVM Instruction Set, the invokevirtual opcode has a value of 182, and it also has a two-byte operand:

invokevirtual
indexbyte1
indexbyte2

Similarly, the JVM will calculate (indexbyte1 << 8) | indexbyte2 into an index. At the index, the run-time constant pool holds a symbolic reference to a CONSTANT_Methodref_info constant. This constant contains the target method information, such as class name, method name, and method descriptor.

The isInstance method requires two elements on the operand stack: The first element is the type; the second element is the object. However, the isAssignableFrom method requires two type elements on the operand stack.

6.4. Summing Up

In summary, let’s use a table to illustrate their differences:

Property instanceof Class.isInstance Class.isAssignableFrom
Kind keyword native method native method
Operands An object and a type A type and an object One type and another type
Null handling false false NullPointerException
Primitive types Not Supported Supported, but always false Yes
Class instance variable No Yes Yes
Bytecode instanceof invokevirtual invokevirtual
Most suitable when The object is given, type is known at compile time The object is given, the target type is not known at compile type No object is given, only types are known and only at runtime
Use cases Daily use, suitable for the majority of cases Complex and untypical cases such as implementing a library or a utility with the use of Reflection API

7. Conclusion

In this tutorial, we looked at the instanceof, Class.isInstance, and Class.isAssignableFrom methods and explored their usage and differences.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)