<!-- mobian-agent-page publisher="dailydev" canonical="https://daily.dev/posts/type-pattern-matching-with-instanceof-dxsiuq9oc" -->

---
title: Type Pattern Matching with instanceof | daily.dev
description: Java's type pattern matching with instanceof combines type testing, casting, and variable declaration into a single expression. Instead of the verbose...
canonical: https://daily.dev/posts/type-pattern-matching-with-instanceof-dxsiuq9oc
twitter:card: summary_large_image
twitter:site: @dailydotdev
og:type: website
og:site_name: daily.dev
og:title: Type Pattern Matching with instanceof | daily.dev
og:description: Java's type pattern matching with instanceof combines type testing, casting, and variable declaration into a single expression. Instead of the verbose...
og:url: https://daily.dev/posts/type-pattern-matching-with-instanceof-dxsiuq9oc
og:image: https://api.daily.dev/og/posts/dXsIUQ9oC.png
og:image:alt: Type Pattern Matching with instanceof
og:image:width: 1200
og:image:height: 630
og:locale: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Type Pattern Matching with instanceof

**[nipafx \(Nicolai Parlog\)](https://daily.dev/sources/nipafx)** · 9 min read · 0 upvotes · 0 comments

## Summary

Java's type pattern matching with instanceof combines type testing, casting, and variable declaration into a single expression. Instead of the verbose instanceof-and-cast idiom, you can write `animal instanceof Elephant elephant` to get a typed, non-null variable scoped to where the check is true. The post covers syntax basics, scoping rules, null safety, the prohibition on upcasting, and practical use cases where OOP polymorphism falls short (e.g., equals() implementations, working with third-party types). It also previews how type patterns will become more powerful when combined with sealed classes and pattern matching in switch expressions.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://nipafx.dev/java-type-pattern-matching>

## Similar posts on daily.dev

- [Non-null variable declaration in Java using instanceof patterns](https://daily.dev/posts/non-null-variable-declaration-in-java-using-instanceof-patterns-jf6lvj2g1) · RUBYLAND · 0 upvotes · 0 comments
- [JEP 530: Primitive Types in Patterns, instanceof, and switch \(Fourth Preview\)](https://daily.dev/posts/jep-530-primitive-types-in-patterns-instanceof-and-switch-fourth-preview--vfd5lcvmr) · Inside Java · 0 upvotes · 0 comments
- [JEP targeted to JDK 27: 532: Primitive Types in Patterns, instanceof, and switch \(5th Preview\) – Inside.java](https://daily.dev/posts/jep-targeted-to-jdk-27-532-primitive-types-in-patterns-instanceof-and-switch-5th-preview-ins-tczuxys4k) · Inside Java · 13 upvotes · 0 comments
- [Java sealed classes and exhaustive pattern matching](https://daily.dev/posts/java-sealed-classes-and-exhaustive-pattern-matching-pahwvvg9e) · Neil Madden · 19 upvotes · 0 comments

---

Tags: [#java](https://daily.dev/tags/java)

[View this post on daily.dev](https://daily.dev/posts/type-pattern-matching-with-instanceof-dxsiuq9oc)

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://daily.dev/#organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180},"sameAs":["https://twitter.com/dailydotdev","https://github.com/dailydotdev","https://www.linkedin.com/company/daily-dev-ltd"]},{"@type":"WebSite","@id":"https://daily.dev/#website","url":"https://daily.dev","name":"daily.dev","publisher":{"@id":"https://daily.dev/#organization"},"potentialAction":{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://daily.dev/search?q={search_term_string}"},"query-input":"required name=search_term_string"}}]}
{"@context":"https://schema.org","@type":"TechArticle","headline":"Type Pattern Matching with instanceof","url":"https://daily.dev/posts/type-pattern-matching-with-instanceof-dxsiuq9oc","mainEntityOfPage":{"@type":"WebPage","@id":"https://daily.dev/posts/type-pattern-matching-with-instanceof-dxsiuq9oc"},"datePublished":"2026-03-16T08:13:38.406Z","dateModified":"2026-03-16T08:22:42.335Z","description":"Java's type pattern matching with instanceof combines type testing, casting, and variable declaration into a single expression. Instead of the verbose...","image":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/5f9154a4f686ec41b1392cb502550d31?_a=AQAEuop","thumbnailUrl":"https://media.daily.dev/image/upload/f_auto,q_auto/v1/posts/5f9154a4f686ec41b1392cb502550d31?_a=AQAEuop","isAccessibleForFree":true,"articleSection":"nipafx (Nicolai Parlog)","inLanguage":"en","publisher":{"@type":"Organization","name":"daily.dev","url":"https://daily.dev","logo":{"@type":"ImageObject","url":"https://daily.dev/apple-touch-icon.png","width":180,"height":180}},"author":{"@type":"Organization","name":"nipafx (Nicolai Parlog)","logo":"https://media.daily.dev/image/upload/s--H_Echo_u--/f_auto,q_auto/v1773648772/logos/nipafx?_a=BAMAMiiu0","url":"https://daily.dev/sources/nipafx"},"commentCount":0,"discussionUrl":"https://daily.dev/posts/type-pattern-matching-with-instanceof-dxsiuq9oc","interactionStatistic":[{"@type":"InteractionCounter","interactionType":{"@type":"LikeAction"},"userInteractionCount":0},{"@type":"InteractionCounter","interactionType":{"@type":"CommentAction"},"userInteractionCount":0}],"keywords":"java","timeRequired":"PT9M"}
{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://daily.dev"},{"@type":"ListItem","position":2,"name":"nipafx (Nicolai Parlog)","item":"https://daily.dev/sources/nipafx"},{"@type":"ListItem","position":3,"name":"Type Pattern Matching with instanceof"}]}
```

