Skip to content(if available)orjump to list(if available)

Java record pattern matching in JDK 19


It's not the feature that excites me, it's the pace of Java language improvements. The 6-month cycle of versions, the whole process of letting experimental features in for a couple versions to test, refine, and likely accept, it's all really working, you know? We're getting stuff that's been so long desired, and it's happening quickly enough to be exciting but not so fast that we're overwhelmed and things are breaking.

People often say that Java 8 was the big important update to the language (streams, lambdas, java.time, and more), but I think Java 9, which began this process was the true rebirth of the language.


Interested in your enthusiasm on this front. If (x instanceof String s) was demonstrated at the JavaOne announcing Java 9. The process is proceeding and visible, but finally getting things so long desired and so long available in other languages isn’t supporting much notion of improved pace. Visibility, yes.


Given that most Java using companies tend to go LTS version to LTS version, it's not like Java 13.141.59 is going to see massive uptake.

And the movement from Java 8 to Java 11 was slowed by bytecode changes and the advent of modules, which declared which of their internals you could dick with. Sure, you could add --add-opens to your commands, but it felt (as intended), hacky as all hell.

For an example, I've submitted PRs to Apache Kafka as part of the effort to replace Powermock with Mockito. Why? Because a) Powermock doesn't play nice with with JUnit 5 and b) Because it breaks totally on Java 16+, due to Java modules being enforced by default as of that version.

But for companies that are happy to keep up with the faster release schedule, there's been access to new and useful features at every new Java version, and it means that early adopters can provide feedback on new features like record classes etc. before they're crystallised in an LTS version.

And given the history of Java improvements between Java 6 and Java 8, the current release cycle is positively blistering in comparison.


> And the movement from Java 8 to Java 11 was slowed by bytecode changes and the advent of modules, which declared which of their internals you could dick with. Sure, you could add --add-opens to your commands, but it felt (as intended), hacky as all hell.

While modules were added in 9, as you mention, strong encapsulation was only turned on in JDK 16; until then, visibility was identical to 8, and no flags were neededl. Modules were not the reason for the difficulties in migration from 8 to 9+. By far, the main reason was libraries that depended on JDK, not subject to backward compatibility, that were changed. Such libraries were non-portable by design.

Strong encapsulation (in effect only since JDK 16) is intended to prevent that. You only need to --add-opens when you bypass the specification, and when you do, you know that your code is not portable. add-opens is hacky because it is needed to enable hackiness; a codebase that isn't hacky doesn't need it.

So now that strong encapsulation is in effect, modules will actually prevent those kinds of difficulties.


> things so long desired and so long available in other languages

That's a feature, not a bug:

When you say, "so long desired," the question is, by whom? Java (the language) isn't aimed at PL enthusiasts, but the majority of programmers, and believe it or not, most programmers are quite averse to new language features, so great care is needed in adopting them if a lanugage wants to be very popular, as Java aims to be.

I can tell you that even when there's a feature we strongly believe is "right", we are wary of adding it to Java until it has been "so long available in other languages." An example I personally worked on — and it's not even a language feature, but a simple API in a single class — is structured concurrency. We love the concept, but because it hasn't yet been "so long available in other languages", we've only proposed it as an "incubator module" to let it mature, and even then we're only adding it because we believe it is too essential to be left out.

It appears that you can be extremely popular or you can be an adventurous innovator/early adopter on the language-feature front, but you can't be both. The Java platform offers multiple language for those who like more adventurous and/or feature-rich languages — and we're happy to accommodate such languages — but they are definitely in the minority (my rough estimate is that only about 10% of programmers prefer such languages).


A lot of the progression on the Java language features occurred after Jigsaw (modules) shipped. From memory it was almost a decade of delays to pretty much every JEP because they couldn’t get Jigsaw and it’s underlying architecture right. I struggle to think of one modern use case for modules.

Kotlin shouldn’t have happened but they let Java lang fester for too long.


Cool to see Java getting these features. After having been a Java dev most of my life, and then moving to Typescript years ago, it's hard to believe I went so long without this.

Related, Typescript is the only language I know of that through flow analysis does not require you to redefine the variable. E.g. you can do something like

  if (typeof foo === 'string') {
    //Typescript knows foo is a string here
All the other languages I know of, like this proposal for Java, require the variable to be redefined. I personally find the Typescript way, as VSCode will give you appropriate type warnings in the right places, easier to use in practice. Just curious if there are any other languages that do it like TS.


Don't be mistaken, Typescript do not has proper pattern matching support, The proposal is far from being adopted

> Related, Typescript is the only language I know of that through flow analysis does not require you to redefine the variable. E.g. you can do something like

This is called smart casting and is widely used in Kotlin


The PL crowd calls that type system feature "occurrence typing."


yes or flow typing It probably overlap with the research on gradual typing too


There are some edge cases because of which this had to be done, I think. For example calling static methods via the instance variables, the actual method called would be the static type of the variable at compile time and not the actual type at runtime:

    public static void main(String[] args) {
        Parent instance = new Child();

        if (instance instanceof Child p) {
            instance.print(); // prints Parent
            p.print(); // prints Child

    static class Parent {
        static void print() {

    static class Child extends Parent {
        static void print() {
Hence, with flow typing, existing code could break in subtle ways.


Several folks have pointed out the poor style in calling static methods using an instance. I think you have a good point, but not a good example. Perhaps a better example is with overload resolution. Consider this Kotlin code:

    fun foo(i: Int) = ...

    fun foo(n: Number) = ...

    fun main() {
        val n: Number = 12;
        foo(n); // calls foo(Number)
        if (n is Int) {
            foo(n); // calls foo(Int)
It's as if smart casting causes the static type of `n` to change in different parts of the main function. As such it also seems to affect overload resolution. Maybe this is exactly what you want. On the other hand it seems like it could lead to some very subtle errors. I'm not a Kotlin programmer, so Kotlin experts please feel free to correct me on details.


Does java provide warnings against calling static methods on Objects?


Yes, if javac is given the `-Xlint:static` option.


If you’re writing code calling static methods on instances of classes, you deserve to have that code broken.


Probably didn’t get written that way but arrived there when that class was refactored somehow.


I've always wondered why that's even allowed.


How does Kotlin solve those use cases?

> instance.print(); // prints Parent

why? the parent print should be shadowed/overidden by the child bruh


statics don't override


Refinement typing (the TS way) is relatively common in type systems that sit on top of dynamic languages because basically they have no choice. Binding to a new variable, however, is a heck of a lot easier to analyse so tends to be done in languages that don’t need to implement refinement typing.

A couple of interesting exceptions: explicit interfaces make it impractical in C#, and there’s a project called liquid Haskell that adds refinement typing on top of an already strong type system.


In my compilers class, this is how we did static downcasting for the toy language we created (I think it was called Oat?). I think it's clever, but I find that I prefer mechanisms that rebind the variable for clarity purposes. It's super easy IMO to accidentally gloss over something as being just a regular if statement rather than something that's actually statically changing the type of the variable compared to having different syntax than just a regular conditional. I also tend to prefer being able to look at a variable's usage and then look back to the time it was initialized and know that it's still the same type; having the type change only within a given scope without any explicit binding seems like something I'd mess up a lot, especially when reviewing code I didn't write myself.


Kotlin does that as well, and it's a JVM language.


Mypy (Python) does this as well. VSCode and PyCharm provide typehints after

   if isinstance(foo, str):
        # code


Sometimes. If foo isn't local scope, this won't work because it could be modified in another thread.


I used to find the workaround pretty jarring:

  lfoo = nonlocal_foo
  if isinstance(lfoo, ...):
Since the checker is correct in these cases, I accepted the ugly and got used to it pretty quickly


Some of this is a consequence of how typescript works - it carries any structural constraints it can find forward in an advisory capacity, because it is somewhat decoupled from the quite-dynamic runtime behavior of the javascript engine.

It isn't even block-based scoping in the flow analysis above - if in your block there was the code foo = 1; the typescript engine would then expect that foo will behave like a Number at runtime.

I haven't dived in deep enough, but I suspect foo could even have different structural type information within the same expression, e.g. a ternary (typeof foo === 'string') ? something(foo) : somethingelse(foo)


It's expression-based as well This doesn't only apply to conditional expressions, but also things like typeof foo === 'string' && ...


Kotlin is the same as typescript.


I am an old school java developer. Can someone explain me why this is a "feature"? I can remember by counting my fingers the times I had to use "instanceof", and it was some classical reflection/injection/instantiation framework 'vodu'. If you are using instanceof in a normal java program, you are just making it wrong. It looks like some new javascript guys arrived in the block and are trying to make java look something messy like Scala. What real world problem is this trying to solve? Are you just fucking bored because you are all out of ideas and every JDK release we need "something"? Why these JEPs only have "boxes,rectangles,A,B,I" class examples and not a simple real world class scenario? Why we need to make java "friendly" to work with generic objects? it should be hell! I cant wait for JDK 50.


Pattern matching is a key feature of functional programming languages; it predates Java by 20 years and remains widely used today. See

For some examples relating to business logic, see

Lots of compilers are written in functional languages and use pattern matching for traversing abstract syntax trees, dispatching on the node type. The OO way to do this is to put all logic relating to a particular class in the class itself. The functional approach is to put all logic for a given method/function in one place and handle all the different types. They are two different philosophies both with different tradeoffs, and which is best depends on the type of program you're writing. But it's nice for a language to support both, so you can pick and choose which approach is best for a given problem.


It shows the majority of its strength in switch statements. You can mostly just use the instanceof for a one line destructuring of a compound object into its constituents. The corresponding rust statement is the `if let` or `if case let` if you are more of a swift fan. Generally you would use it with sum types which were previously extremely nasty to work with in Java but are more useful now with sealed classes.

The pattern shows up a lot with result and option types but you can do it any other time where a tagged union makes sense. The classic way to do a tagged union in Java was just a personal taste amongst bad options: tagging interface and instanceof on implementations or an encapsulating class that will return null (or throw an exception) if the gets are called for the types which aren't encapsulated.

If in your career you never needed to enumerate cases that required different data in each case and you couldn't see how binding those requirements into an invariant via the type system was helpful then you probably won't use these new features either. Some people thought it was helpful but the boilerplate cost was too high so wouldn't do it in practice (I am in this category).

Edit: you could also make tagged unions more type safe with callbacks, but then implementing a closure to pull the value out of the callback was just annoying.


"If in your career you never needed to enumerate cases that required different data..." . Maybe one or two times... but it is not like "hey, it is impossible", or "shit, I have to write 10 more lines" in this rare code I will never put my eyes again. What I get angry is that people will start to use everywhere, in places they should not be using. because the guy did not wanted to stop five minutes to think about it. The river flows through the easiest path, but this does not means it is the smartest path.


Unfortunately, I've seen plenty of Java 8 code where people did not "stop five minutes to think about it". I don't think any language can protect us from ourselves.

It's fair that you've found sensible ways to achieve your goals without ever needing pattern matching. Nobody should fault you for that. But can you grant that other developers might have other sensible ways to achieve other goals (or sometimes other ways to achieve the same goals!)?

I think most Java developers will be familiar with the visitor pattern. In almost all cases (there are exceptions!), I detest it; I find sum types with pattern matching to be a far superior way to say what I mean. Java added `sealed` interfaces recently, so you can actually model a closed family of types. Now pattern matching closes the loop, giving you a supremely ergonomic way of dispatching over that closed family without using a visitor.

Maybe it's not clear from other responses, but the part of the JEP about an "exhaustive switch" is critical here. Java statically guarantees that you've handled each member of your closed family, just like if you forgot to implement a method for a visitor.


"...look something messy like Scala", I feel bad you feel this way as Scala is an incredibly powerful and elegant language.

It's ironic because you're throwing shade at Javascript when JS developers are doing the same thing against Typescript. Fearing what you don't know or understand under the guise that change is bad or things are "good enough".


I agree Scala is very powerful. I said messy because it is "too powerful", added too many features. I think this is their mistake. I respect the community, but can show my opinion as you can about java.


I really don’t feel that Scala has too many features. It is a relatively small language which is very elegant due to having almost no edge cases. Sure, due to not having many features yet being very expressive it has to have very powerful language primitives that can be abused, but I really don’t think that it is as bad as its name.


I really liked scala… until I had a to maintain a large and « old » code base in it.

I think messy fits.


Java is also a messy language and the reason for a lot of the mess in Scala. If you want elegance, then you should probably learn something like Haskell.


> show my opinion as you can about java.

Showing your opinion doesn't make it immune from critique.


Because there are scenarios where pattern matching (or instanceof) is the better and more ergonomic thing to use. One obvious example I can think of is event handling.

If anything, this is _not_ something JS devs are asking for, but rather devs using functional languages.


I also think of API ergonomics. Unspecified input with the same single endpoint, while under the hood will be absolute spaghetti, from a developer adoption standpoint can be make or break.

And if you're strongly typing your system rather than relying on strings, extra so.



Exactly, this is what OP was trying to point out. With the record matching feature, you don't need the extra abstract base class anymore.

This is not to say that abstract base classes are never a good idea, but sometimes in Java they were only there for the convenience of one handler method, to prevent the use of instanceof. This didn't improve readability. That's when the record matching can be a good improvement.


Sometimes it isn't desirable to scatter the logic across many classes, or conflate the data and the handling. The Java world has the "visitor pattern" to help deal with that, but its double-dispatch is clunky, complex and verbose. Pattern matching just generalises switch and makes it more useful. Java is genuinely a better language with this change.


I wish they had better examples, so here is one. Suppose you are making a scene graph, and you want to have a visitor and run code based on the type of the nodes in the graph. Well, consider two approaches. The first approach is to use a bunch of ifs and instanceof, but this isn't clean and it is fragile. The second approach is to make an interface that has all the types listed under a handler (void handle(Type1 t), void handle(Type2 t), ...) and then write some boiler plate code to do the dispatch.

This feature aims at the conciseness of first model (instanceof) with the safety of the second approach (i.e. all types are handled).

I would love this feature for my project! ( ) since I use Java to implement a programming language.


This is the point, you dont consider the instanceof approach, is just wrong. You already know your types in advance.


The sealed interface, final implementation case is an interesting one to inspect. Now you can, for example, write a parser with a fixed set of tokens and write switches that exhaustively handle every token. You now get compile time guarantees.



You really could do away with the arrogance.


It's just an example...

And you don't seem to understand that it's about the safety of the exhaustive check not just the sugar.


> If you are using instanceof in a normal java program

In applications yes, but instanceof is heavily used if you write frameworks.

And as someone else has mentioned, the more functional style java you write the more use you start to make of instanceof


Highly compositional design patterns in Java. When you have a fairly large set of custom types that are hierarchically structued in nature, it can be quite useful.

In most standard IT applications it may be less common. I've seen it used fairly well in scientific applications. There are of course other ways of approaching working with and acting on wide taxonomies of objects that could exist but this is one of them.


Coming from Scala, I find positional decomposition a huge mistake.

In Scala, constructing a `case class` allows positional/named arguments, which prevents some bugs. But destructring has to be positional. Where is the mathematical beauty of duality?

Also if I only want a few fields from a wide record, I have to add the underscores for all other fields. `case SomeClass(_, _, _, field1, field2) =>` And when I add another field, all those `case`s have to be changed.

OCaml gets it right. Pattern matching records uses named fields. It has field punning, you only have to write the name once if new variable's name is the field name (like JS). Extra fields are discarded with one single `; _` in the pattern.

I don't really care what Java does. I just hope Kotlin won't mess it up.


A great paper on this topic:

D. J. Pearce, “Sound and Complete Flow Typing with Unions, Intersections and Negations,” in Verification, Model Checking, and Abstract Interpretation, vol. 7737, R. Giacobazzi, J. Berdine, and I. Mastroeni, Eds. Berlin, Heidelberg: Springer Berlin Heidelberg, 2013, pp. 335–354. doi: 10.1007/978-3-642-35873-9_21.


I really feel Java already has everything I need to do my work. More improvements are always welcome of course.


Me too. I do not like records or pattern matching in Java. We have scala/kotlin/groovy for this.

Java should have focused more on improving libraries, build infra (Java 9 was good) and changes in syntax focused on the traditional paradigm. If people needed pattern matching. Thy could mix languages. This is huge.

I find myself more and more distant from java these days because of this.


Records, pattern matching and the like are eons better than POJOs manipulated by reflection. Especially once value types come and many record type can be propagated as a value type, it will also cause huge speed improvements.


The demonstration on youtube is really nice. The 500 x 500 matrix of objects showed a really fast speed up.

I suspect though that internally theres still a lot of plumbing that needs to be changed on top of what they've already changed. It was the same way for reflection, lambdas, threads. They really do a great job on iterating and keeping backwards compatability too.


Great! Java is getting destructuring.

    static void printUpperLeftColoredPoint(Rectangle r) {
        if (r instanceof Rectangle(ColoredPoint ul, ColoredPoint lr)) {

But lr is not used in this example. Where is the syntax for ignoring a component? Can we drop lr, or use a placeholder like '_' multiple times?


_ is reserved likely for this use case in advance, but AFAIK it is not yet settled on how exactly should it work.

But as always, the language team does an excellent job on improvements which is exceedingly hard while you have to balance backward compatibility.


For everyone saying the visitor pattern requieres switch statements this is false. The visitor can build its case inside the iterface/implemetation. If the complexity is being solved by the "housing" class that the visitor will be visiting you are not doing enough.


I love seeing modern language features coming to Java! Sadly, I suspect it will be at least a decade before I see Java 19 used anywhere I work...


Despite recent improvements including raw string literals it beggars belief how Java still requires regex metacharacters to be escaped. Until that's fixed Java is not an option for me.




Another feature, that Java finally adopts, after decades of not adopting it. More and more language features and concepts are being introduced, finally exposing Java-only programmers to them. Lambdas, pattern matching, project Loom, at some point in the future, Java might be called a modern language. Good for Java and Java programmers.

Just checked some other languages:

- SRFI (Scheme Request for Implementation):


> | ($ record-name pat_1 ... pat_n) a record

- GNU Guile:

Seems to be implementing the SRFI:

> | ($ record-name pat_1 ... pat_n) a record

- Rust: and

> We can also use patterns to destructure structs, enums, and tuples to use different parts of these values. Let’s walk through each value.

> Struct patterns match struct values that match all criteria defined by its subpatterns. They are also used to destructure a struct.


> Another feature, that Java finally adopts, after decades of not adopting it.

Back in 1997, James Gosling published an article that serves as a blueprint to Java's evolution to this day: He explained it further, as related by Brian Goetz in the first 20 minutes of this talk:

In short, Java attempts to be "a wolf in sheep's clothing," with a state-of-the-art, innovative runtime, wrapped in a language that's intentionally conservative. Java the language only adopts features that have already proven themselves as worthwhile elsewhere, and when mainstream programmers are ready for them. We don't always live up to that standard, but we try.

So another way of putting what you said is that this is another feature that has proven itself enough, for long enough, and that's matured enough for Java to adopt it. Many if not most programming language features don't make it to that stage.

The flip side is to examine which features have never made it to Java and probably never will (at least, they're not on our long-term roadmap). Those include, among others, macros, extension methods, first-class properties, and async/await.




> async/await

Why would those be considered in the first place especially after Loom lands?


It was considered before.


there is nothing to be proud of about lacking extension methods. Kotlin has them and it allow to make amazing APIs that are much more ergonomic/reduce cognitive overhead.


I personally think there's much to be proud of in lacking any feature, as long as you deliver what your users expect, especially if the number of users is very large. After all, the goal of a mainstream programming language is not to have as many features as possible, but as few as necessary, where "necessary" takes into account both the common software requirements of the era (which are also shaped by the hardware characteristics of the era) as well as the expectations and habits of the majority of programmers at that point in time.

I can tell you that if Java's language team encounters some language feature that solves a particular problem, they'd rather spend spend several years thinking how to avoid adding that feature while still addressing the problem, than the six months needed to implement it. A good strategy for doing that is to wait, and try to think of ways to solve multiple problems with one feature (even if imperfectly) rather than solve multiple problems with multiple features.


The same feature does not entail the same tradeoff depending on the languages.

For Kotlin, extensions methods are essential otherwise you can not extend existing Java types with Kotlin types.

For C#, you need extension methods for LINQ because modifying the .Net runtime was not an option at that time.

For Java, changing the VM is not an issue, instead of extension methods you add default methods which can be overridden by implementations.

Same feature, different tradeoffs.


I think Kotlin went the easy way with extension methods. Scala’s solution is much more elegant with implicits (especially now with Scala 3).


What I love about Java though, is that this new syntax is very clear and its intentions are obvious when you're reading someone else's code. It doesn't require you to hold a mountain of context in your head at all times like Swift and Kotlin do. Yes, it's verbose at times. But verbosity is a good property for a programming language because it allows the code to be read and understood with ease outside of an IDE.


Most Java is very easy to comprehend, but then you have things like implicit finals and whatever you call the `List<? extends Something>` language construct. I think most Java is very boring, functional code with some notable exceptions related to threading and the typing system. It gets the job done, but often in an ugly, roundabout way.

The way `Optional` is implemented and the roundabout way to just grab the first item in a list (`` or `var e = null; if (!list.isEmpty()) e = list.get(0);`?) where other languages have added helpers years ago. Or, even worse, the lack of tuples, leading to your average medium-sized Java project containing five different implementations of Tuple/Pair. There are also runtime restrictions that sometimes crop up because Oracle can't break compatibility (type erasure, for example).

If code clarity was the only metric for language quality then we'd all be writing some BASIC derivative. I don't think more modern languages like Swift and Kotlin are all that applicable to all areas where Java shines, but ever since dotnet went open source and cross platform I'm really starting to wonder why anyone still bothers with Java.


I personally feel that tuples are an antipattern. It’s relatively simple to just define a record with the properties you need and gives meaningful context information because the record and it’s properties are named.


I was almost with you up until the end then I had to just shake my head. I mean, it's readable syntax and doesn't require some crazy new sigils or operators, it's "fine" though I'm also shaking my head at the scope rules for these bindings. Similarly I'm ok with the "fine" syntax of Optional, and being explicit with my Optional.orElse()'s, rather than introducing an 'elvis operator' or some bikeshedded derivative. Though I wouldn't mind such things -- I'm glad Java keeps evolving useful things anyway, but it does wear on you (or at least me) to have to speak them (or even to tell your IDE to speak them on your behalf) as if your mouth was full of sand or to read them as if they're a high school student's essay obviously padded to reach some word count minimum.

If I came across this new thing organically, I could go "ah, neat, we have that now" and not necessarily need to go look up the JEP (meanwhile Python's PEP 636 had me going wtf) -- though over time I'd expect to come across it in a negative context where the original programmer made a scope mistake because they didn't read the JEPs, or because they tried to make a change without their IDE's assistance where through various means it could have pointed out shadowing or scope concerns, that I now have to fix.

It's in no minor part thanks to Java's verbosity that the overall Java ecosystem is so verbose. Having mostly non-confusing syntax whose meaning you can mostly guess at on first exposure is nice, yes, but isn't so helpful for the more important aspect of having non-confusing programs. For that you really want a more concise and tasteful language over a verbose one. Anyway, I've found that for anything non-trivial about the program itself, I'm going to need an IDE because the meat of the thing is going to be verbosely spread out in many places (sometimes for good reasons, at least in the Java world). Sure, after I acquire the mountain of (program) context, I can review small changes even on paper printouts, but that's true of most anything.

Verbosity is not a good property for a language to have in general -- Java itself admits this, otherwise we wouldn't have so many shortcuts in syntax like omitting this, or java 7's diamond operator or try-with-resources or catching multiple exceptions or for-each syntax, or java 8's lambdas and special syntax for simple lambdas, or java 9's var, or... And of course, almost always in some other more concise language a concept is much clearer. There's a reason pseudo-code isn't written to look like Java.


> Another feature, that Java finally adopts, after decades of not adopting it

Java has been steadily moving in this direction for quite a few years now. It's tricky business. There's the language proper, and there's the JVM, which are interdependent. Both are aiming to remain backwards compatible. Introducing new major language features is not an easy feat.


C# has been adding new major language features all the time since its inception, including pretty much everything we are seeing being added to Java these days.


Yeah, and this "kitchen sink" approach is partly why I stopped writing C#. Same thing is turning me away from JS.

It's exhausting trying to stay up-to-date with the language, in addition to changes in the frameworks and tools on top of the challenges of my day job.

I might be showing my age, but I've come to appreciate that features come after long and careful consideration, and not every single release.