SlideShare ist ein Scribd-Unternehmen logo
1 von 40
Downloaden Sie, um offline zu lesen
The Future of Java: Records, Sealed Classes
and Pattern Matching
Among other things
José Paumard
Java Developer Advocate
Java Platform Group
https://twitter.com/JosePaumard
https://github.com/JosePaumard
https://www.youtube.com/user/java
https://www.youtube.com/user/JPaumard
https://www.youtube.com/hashtag/jepcafe
https://fr.slideshare.net/jpaumard
https://www.pluralsight.com/authors/jose-
paumard
https://dev.ja
va
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
3
Dev.java
Java 8 Java 11
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
4
Java 17 – LTS!
Java 8 Java 11
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
5
Java 17 – LTS!
Java 8
Java 11
Java 17 ?
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
6
Next LTS: Java 21
Java 8
Java 11
Java 17
Java 21
Language
Type inference for locals (var)
Switch expressions
Text blocks
Record classes
Sealed classes
Pattern matching for instanceof
Compact String, Indify
Concatenation
JDK 17: New Features Since the JDK 8
Copyright © 2021, Oracle and/or its affiliates
7
Tools
jshell
jlink
jdeps
jpackage
java source code launcher
javadoc search + API history
JVM
Garbage Collectors: G1, ZGC
AArch64 support: Windows, Mac, Linux
Docker awareness
Class Data Sharing by default
Helpful NullPointerExceptions
Hidden classes
Libraries
HTTP client
Collection factories
Unix-domain sockets
Stack walker
Deserialization filtering
Pseudo-RNG, SHA-3, TLS 1.3
3/11/2022
Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted
8
Stop doing that!
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
9
Do not call new Integer(...)
Stop Calling Wrapper Classes Constructors!
@Deprecated(since="9", forRemoval = true)
public Integer(int value) {
this.value = value;
}
@IntrinsicCandidate
public static Integer valueOf(int i) {
// some code
return new Integer(i);
}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
10
Do not override finalize()
Stop Overriding Finalize!
@Deprecated(since="9")
protected void finalize() throws Throwable {
}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted
11
Records
?Record and Array Pattern Matching?
Record
Sealed Classes
Switch Expression
Constant Dynamic
Inner Classes
private in VM
Nestmates
Pattern Matching for instanceof
11
14
16
17 Switch on Patterns
19
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
13
In reference to the Amber
Chronicles by Roger Zelazny
Project Amber
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
14
Record Deconstruction
if (o instanceof Rectangle rectangle) {
int width = rectangle.width();
int height = rectangle.height();
// do something with width and height
}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
15
This is record deconstruction
width are height are binding variables
Record Deconstruction
if (o instanceof Rectangle(int width, int height)) {
// do something with width and height
}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
16
Three type patterns
Three pattern binding variables
One target operand
Pattern Matching for Switch
String formatted =
switch(number) {
case Integer i -> String.format("int %d", i);
case Long l -> String.format("long %d", l);
case Double d -> String.format("double %d", d);
}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
17
Pattern Matching for Switch
record Square(int edge) {}
record Circle(int radius) {}
double area = switch(shape) {
case Square(int edge) -> edge* edge;
case Circle(int radius) -> Math.PI*radius*radius;
default -> ...;
}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
18
Array Pattern Matching
if (o instanceof String[] array && array.length() >= 2) {
// do something with array[0] and array[1]
}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
19
Array Pattern Matching
if (o instanceof String[] {String s1, String s2}) {
// do something with s1 and s2
}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
20
Array Pattern Matching
if (o instanceof Circle[] {Circle(var r1), Circle(var r3)}) {
// do something with r1 and r2
}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
21
You can use var in patterns
Syntaxic Sugars
if (shape instanceof Circle(var center, var radius)) {
// center and radius are binding variables
}
record Point(int x, int y) {}
record Circle(Point center, int radius) implements Shape {}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
22
You can tell that you do not need a binding variable
Syntaxic Sugars
if (shape instanceof Circle(var center, _)) {
// center and radius are binding variables
}
record Point(int x, int y) {}
record Circle(Point center, int radius) implements Shape {}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
23
You can nest patterns (nested patterns)
Syntaxic Sugars
if (shape instanceof Circle(var center, _) &&
center instanceof Point(int x, int y)) {
// center and radius are binding variables
}
if (shape instanceof Circle(Point(int x, int y), _)) {
// center and radius are binding variables
}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
24
The deconstruction uses the canonical constructor of a
record
What about:
- factory methods?
- classes that are not records?
Deconstruction
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
25
Deconstruction Using Factory Methods
interface Shape {
static Circle circle(double radius) {
return new Circle(radius);
}
static Square square(double edge) {
return new Square(edge);
}
}
record Circle(double radius) {}
record Square(double edge) {}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
26
Then this code becomes possible:
Deconstruction Using Factory Methods
double area = switch(shape) {
case Shape.circle(double radius) -> Math.PI*radius*radius;
case Shape.square(double edge) -> edge*edge;
}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
27
What About Your POJOs?
public class Point {
private int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public deconstructor(int x, int y) {
x = this.x;
y = this.y;
}
}
The binding variables
are the same
external state
description
Allows defensive copy
and overloading
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
28
You saw patterns with instanceof and switch
Let us see match !
Pattern with Match
record Point(int x, int y) {}
record Circle(Point center, int radius) implements Shape {}
Circle circle = ...;
match Circle(var center, var radius) = circle;
// center and radius are binding variables
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
29
If shape is in fact a rectangle…
You can throw an exception
Pattern with Match
Shape shape = ...;
match Circle(var center, var radius) = shape
else
throw new IllegalStateException("Not a circle");
// center and radius are binding variables
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
30
If shape is in fact a rectangle …
Or define default values
Pattern with Match
Shape shape = ...;
match Circle(Point center, int radius) = shape
else {
center = new Point(0, 0); // this is called
radius = 1d; // an anonymous matcher
}
// center and radius are binding variables
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
31
You can use match with more than one pattern…
… or use nested patterns
Pattern with Match
Shape shape = ...;
match Rectangle(var p1, var p2) = shape,
Point(var x0, var y0) = p1,
Point(var x1, var y2) = p2;
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
32
With factory methods
More Examples
if (opt instanceof Optional.of(var max)) {
// max is a binding variable
}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
33
With factory methods
More Examples
if (s instanceof
String.format("%s is %d years old",
String name, Integer.valueOf(int age) {
// name and age are binding variables
}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
34
You can create maps with factory methods
This is an extended form of Pattern Matching where you
check the value of a binding variable
More Examples
if (map instanceof Map.withMapping("name", var name) &&
map instanceof Map.withMapping("email", var email)) {
// name and email are binding variables
}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
35
Pattern combination
More Examples
if (map instanceof Map.withMapping("name", var name) __AND
map instanceof Map.withMapping("email", var email)) {
// name and email are binding variables
}
__AND = pattern combination
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
36
More Examples
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address" : {
"street": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
}
}
if (json instanceof
stringKey("firstName", var firstName) __AND
stringKey("lastName", var lastName) __AND
intKey("age", var age) __AND
objectKey("address",
stringKey("stree", var street) __AND
stringKey("city", var city) __AND
stringKey("state", var state)
)) {
// firstName, lastName, age,
// street, city, state, ...
// are binding variables
}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
37
If Java Embraces « Map Literals »
Map<String, String> map = {
"firstName": "John",
"lastName": "Smith",
"age": "25"
}
if (map instanceof
{
"firstName": var firstName,
"lastName": var lastName,
"age": Integer.toString(var age)
}) {
// firstName, lastName, age
// are binding variables
}
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
38
• Constant Patterns: checks the operand with a constant
value
• Type Patterns: checks if the operand has the right type,
casts it, and creates a binding variable
Patterns at a Glance
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
39
• Patterns + Deconstruction: checks the operand type,
casts it, bind the component to binding variables
• Patterns + Method: uses a factory method or a
deconstructor
• Patterns + Var: infers the right type, and creates the
binding variable
• Pattern + _: infers the right type, but does not create
the binding variable
Patterns at a Glance
3/11/2022
Copyright © 2021, Oracle and/or its affiliates |
40
Where are we?
• Pattern Matching for instanceof
• Pattern Matching for Switch
• Record and Array Pattern Matching
• Match
• Literals
Patterns at a Glance

Weitere ähnliche Inhalte

Was ist angesagt?

Hibernate
Hibernate Hibernate
Hibernate Sunil OS
 
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...rehaniltifat
 
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...
Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...Philip Schwarz
 
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...Edureka!
 
Les03 (Using Single Row Functions To Customize Output)
Les03 (Using Single Row Functions To Customize Output)Les03 (Using Single Row Functions To Customize Output)
Les03 (Using Single Row Functions To Customize Output)Achmad Solichin
 
The Play Framework at LinkedIn
The Play Framework at LinkedInThe Play Framework at LinkedIn
The Play Framework at LinkedInYevgeniy Brikman
 
MySQL Database Architectures - InnoDB ReplicaSet & Cluster
MySQL Database Architectures - InnoDB ReplicaSet & ClusterMySQL Database Architectures - InnoDB ReplicaSet & Cluster
MySQL Database Architectures - InnoDB ReplicaSet & ClusterKenny Gryp
 
Getting Started with MySQL I
Getting Started with MySQL IGetting Started with MySQL I
Getting Started with MySQL ISankhya_Analytics
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in javaAdil Mehmoood
 
Introduction to kotlin and OOP in Kotlin
Introduction to kotlin and OOP in KotlinIntroduction to kotlin and OOP in Kotlin
Introduction to kotlin and OOP in Kotlinvriddhigupta
 
Power JSON with PostgreSQL
Power JSON with PostgreSQLPower JSON with PostgreSQL
Power JSON with PostgreSQLEDB
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot IntroductionJeevesh Pandey
 
Arrays in Java | Edureka
Arrays in Java | EdurekaArrays in Java | Edureka
Arrays in Java | EdurekaEdureka!
 
Decision making in JAVA
Decision making in JAVADecision making in JAVA
Decision making in JAVAHamna_sheikh
 

Was ist angesagt? (20)

Hibernate
Hibernate Hibernate
Hibernate
 
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
03 Writing Control Structures, Writing with Compatible Data Types Using Expli...
 
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...
Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...Algebraic Data Types forData Oriented Programming - From Haskell and Scala t...
Algebraic Data Types for Data Oriented Programming - From Haskell and Scala t...
 
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
 
Les03 (Using Single Row Functions To Customize Output)
Les03 (Using Single Row Functions To Customize Output)Les03 (Using Single Row Functions To Customize Output)
Les03 (Using Single Row Functions To Customize Output)
 
Log4 J
Log4 JLog4 J
Log4 J
 
The Play Framework at LinkedIn
The Play Framework at LinkedInThe Play Framework at LinkedIn
The Play Framework at LinkedIn
 
MySQL Database Architectures - InnoDB ReplicaSet & Cluster
MySQL Database Architectures - InnoDB ReplicaSet & ClusterMySQL Database Architectures - InnoDB ReplicaSet & Cluster
MySQL Database Architectures - InnoDB ReplicaSet & Cluster
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
Getting Started with MySQL I
Getting Started with MySQL IGetting Started with MySQL I
Getting Started with MySQL I
 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
 
Introduction to kotlin and OOP in Kotlin
Introduction to kotlin and OOP in KotlinIntroduction to kotlin and OOP in Kotlin
Introduction to kotlin and OOP in Kotlin
 
Scala Intro
Scala IntroScala Intro
Scala Intro
 
Power JSON with PostgreSQL
Power JSON with PostgreSQLPower JSON with PostgreSQL
Power JSON with PostgreSQL
 
Spring data jpa
Spring data jpaSpring data jpa
Spring data jpa
 
Spring boot Introduction
Spring boot IntroductionSpring boot Introduction
Spring boot Introduction
 
Arrays in Java | Edureka
Arrays in Java | EdurekaArrays in Java | Edureka
Arrays in Java | Edureka
 
Decision making in JAVA
Decision making in JAVADecision making in JAVA
Decision making in JAVA
 
Data classes in kotlin by Naveed
Data classes in kotlin by NaveedData classes in kotlin by Naveed
Data classes in kotlin by Naveed
 
Introduction to Java
Introduction to JavaIntroduction to Java
Introduction to Java
 

Ähnlich wie The Future of Java: Records, Sealed Classes and Pattern Matching

Building data fusion surrogate models for spacecraft aerodynamic problems wit...
Building data fusion surrogate models for spacecraft aerodynamic problems wit...Building data fusion surrogate models for spacecraft aerodynamic problems wit...
Building data fusion surrogate models for spacecraft aerodynamic problems wit...Shinwoo Jang
 
Object - Oriented Programming: Inheritance
Object - Oriented Programming: InheritanceObject - Oriented Programming: Inheritance
Object - Oriented Programming: InheritanceAndy Juan Sarango Veliz
 
ON SOME FIXED POINT RESULTS IN GENERALIZED METRIC SPACE WITH SELF MAPPINGS UN...
ON SOME FIXED POINT RESULTS IN GENERALIZED METRIC SPACE WITH SELF MAPPINGS UN...ON SOME FIXED POINT RESULTS IN GENERALIZED METRIC SPACE WITH SELF MAPPINGS UN...
ON SOME FIXED POINT RESULTS IN GENERALIZED METRIC SPACE WITH SELF MAPPINGS UN...IRJET Journal
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3Naga Muruga
 
JS Fest 2018. Виталий Ратушный. ES X
JS Fest 2018. Виталий Ратушный. ES XJS Fest 2018. Виталий Ратушный. ES X
JS Fest 2018. Виталий Ратушный. ES XJSFestUA
 
Refactoring - improving the smell of your code
Refactoring - improving the smell of your codeRefactoring - improving the smell of your code
Refactoring - improving the smell of your codevmandrychenko
 
SECTION D2)Display the item number and total cost for each order l.docx
SECTION D2)Display the item number and total cost for each order l.docxSECTION D2)Display the item number and total cost for each order l.docx
SECTION D2)Display the item number and total cost for each order l.docxkenjordan97598
 
JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020Joseph Kuo
 
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdfRihabBENLAMINE
 
On best one sided approximation by multivariate lagrange
      On best one sided approximation by multivariate lagrange      On best one sided approximation by multivariate lagrange
On best one sided approximation by multivariate lagrangeAlexander Decker
 
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of TonguesChoose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of TonguesCHOOSE
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQLDHAAROUN
 

Ähnlich wie The Future of Java: Records, Sealed Classes and Pattern Matching (20)

Building data fusion surrogate models for spacecraft aerodynamic problems wit...
Building data fusion surrogate models for spacecraft aerodynamic problems wit...Building data fusion surrogate models for spacecraft aerodynamic problems wit...
Building data fusion surrogate models for spacecraft aerodynamic problems wit...
 
Object - Oriented Programming: Inheritance
Object - Oriented Programming: InheritanceObject - Oriented Programming: Inheritance
Object - Oriented Programming: Inheritance
 
ON SOME FIXED POINT RESULTS IN GENERALIZED METRIC SPACE WITH SELF MAPPINGS UN...
ON SOME FIXED POINT RESULTS IN GENERALIZED METRIC SPACE WITH SELF MAPPINGS UN...ON SOME FIXED POINT RESULTS IN GENERALIZED METRIC SPACE WITH SELF MAPPINGS UN...
ON SOME FIXED POINT RESULTS IN GENERALIZED METRIC SPACE WITH SELF MAPPINGS UN...
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
 
JS Fest 2018. Виталий Ратушный. ES X
JS Fest 2018. Виталий Ратушный. ES XJS Fest 2018. Виталий Ратушный. ES X
JS Fest 2018. Виталий Ратушный. ES X
 
Refactoring - improving the smell of your code
Refactoring - improving the smell of your codeRefactoring - improving the smell of your code
Refactoring - improving the smell of your code
 
SECTION D2)Display the item number and total cost for each order l.docx
SECTION D2)Display the item number and total cost for each order l.docxSECTION D2)Display the item number and total cost for each order l.docx
SECTION D2)Display the item number and total cost for each order l.docx
 
6-TDD
6-TDD6-TDD
6-TDD
 
JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020
 
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
10_interfacesjavaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.pdf
 
Ch3
Ch3Ch3
Ch3
 
On best one sided approximation by multivariate lagrange
      On best one sided approximation by multivariate lagrange      On best one sided approximation by multivariate lagrange
On best one sided approximation by multivariate lagrange
 
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of TonguesChoose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
 
Java
JavaJava
Java
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
Introduction to SQL
Introduction to SQLIntroduction to SQL
Introduction to SQL
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 
Ch 3.pdf
Ch 3.pdfCh 3.pdf
Ch 3.pdf
 
ch3.ppt
ch3.pptch3.ppt
ch3.ppt
 

Mehr von José Paumard

Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19José Paumard
 
Deep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UKDeep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UKJosé Paumard
 
Designing functional and fluent API: application to some GoF patterns
Designing functional and fluent API: application to some GoF patternsDesigning functional and fluent API: application to some GoF patterns
Designing functional and fluent API: application to some GoF patternsJosé Paumard
 
The Sincerest Form of Flattery
The Sincerest Form of FlatteryThe Sincerest Form of Flattery
The Sincerest Form of FlatteryJosé Paumard
 
The Sincerest Form of Flattery
The Sincerest Form of FlatteryThe Sincerest Form of Flattery
The Sincerest Form of FlatteryJosé Paumard
 
Designing functional and fluent API: example of the Visitor Pattern
Designing functional and fluent API: example of the Visitor PatternDesigning functional and fluent API: example of the Visitor Pattern
Designing functional and fluent API: example of the Visitor PatternJosé Paumard
 
Construire son JDK en 10 étapes
Construire son JDK en 10 étapesConstruire son JDK en 10 étapes
Construire son JDK en 10 étapesJosé Paumard
 
Java Keeps Throttling Up!
Java Keeps Throttling Up!Java Keeps Throttling Up!
Java Keeps Throttling Up!José Paumard
 
Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2José Paumard
 
Lambda and Stream Master class - part 1
Lambda and Stream Master class - part 1Lambda and Stream Master class - part 1
Lambda and Stream Master class - part 1José Paumard
 
Asynchronous Systems with Fn Flow
Asynchronous Systems with Fn FlowAsynchronous Systems with Fn Flow
Asynchronous Systems with Fn FlowJosé Paumard
 
JAX-RS and CDI Bike the (Reactive) Bridge
JAX-RS and CDI Bike the (Reactive) BridgeJAX-RS and CDI Bike the (Reactive) Bridge
JAX-RS and CDI Bike the (Reactive) BridgeJosé Paumard
 
Collectors in the Wild
Collectors in the WildCollectors in the Wild
Collectors in the WildJosé Paumard
 
JAX RS and CDI bike the reactive bridge
JAX RS and CDI bike the reactive bridgeJAX RS and CDI bike the reactive bridge
JAX RS and CDI bike the reactive bridgeJosé Paumard
 
L'API Collector dans tous ses états
L'API Collector dans tous ses étatsL'API Collector dans tous ses états
L'API Collector dans tous ses étatsJosé Paumard
 
Linked to ArrayList: the full story
Linked to ArrayList: the full storyLinked to ArrayList: the full story
Linked to ArrayList: the full storyJosé Paumard
 

Mehr von José Paumard (20)

Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19Loom Virtual Threads in the JDK 19
Loom Virtual Threads in the JDK 19
 
Deep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UKDeep Dive Java 17 Devoxx UK
Deep Dive Java 17 Devoxx UK
 
Designing functional and fluent API: application to some GoF patterns
Designing functional and fluent API: application to some GoF patternsDesigning functional and fluent API: application to some GoF patterns
Designing functional and fluent API: application to some GoF patterns
 
The Sincerest Form of Flattery
The Sincerest Form of FlatteryThe Sincerest Form of Flattery
The Sincerest Form of Flattery
 
The Sincerest Form of Flattery
The Sincerest Form of FlatteryThe Sincerest Form of Flattery
The Sincerest Form of Flattery
 
Designing functional and fluent API: example of the Visitor Pattern
Designing functional and fluent API: example of the Visitor PatternDesigning functional and fluent API: example of the Visitor Pattern
Designing functional and fluent API: example of the Visitor Pattern
 
Construire son JDK en 10 étapes
Construire son JDK en 10 étapesConstruire son JDK en 10 étapes
Construire son JDK en 10 étapes
 
Java Keeps Throttling Up!
Java Keeps Throttling Up!Java Keeps Throttling Up!
Java Keeps Throttling Up!
 
Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2Lambdas and Streams Master Class Part 2
Lambdas and Streams Master Class Part 2
 
Lambda and Stream Master class - part 1
Lambda and Stream Master class - part 1Lambda and Stream Master class - part 1
Lambda and Stream Master class - part 1
 
Asynchronous Systems with Fn Flow
Asynchronous Systems with Fn FlowAsynchronous Systems with Fn Flow
Asynchronous Systems with Fn Flow
 
Java Full Throttle
Java Full ThrottleJava Full Throttle
Java Full Throttle
 
JAX-RS and CDI Bike the (Reactive) Bridge
JAX-RS and CDI Bike the (Reactive) BridgeJAX-RS and CDI Bike the (Reactive) Bridge
JAX-RS and CDI Bike the (Reactive) Bridge
 
Collectors in the Wild
Collectors in the WildCollectors in the Wild
Collectors in the Wild
 
Streams in the wild
Streams in the wildStreams in the wild
Streams in the wild
 
JAX RS and CDI bike the reactive bridge
JAX RS and CDI bike the reactive bridgeJAX RS and CDI bike the reactive bridge
JAX RS and CDI bike the reactive bridge
 
Free your lambdas
Free your lambdasFree your lambdas
Free your lambdas
 
L'API Collector dans tous ses états
L'API Collector dans tous ses étatsL'API Collector dans tous ses états
L'API Collector dans tous ses états
 
Linked to ArrayList: the full story
Linked to ArrayList: the full storyLinked to ArrayList: the full story
Linked to ArrayList: the full story
 
Free your lambdas
Free your lambdasFree your lambdas
Free your lambdas
 

Kürzlich hochgeladen

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 

Kürzlich hochgeladen (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 

The Future of Java: Records, Sealed Classes and Pattern Matching

  • 1. The Future of Java: Records, Sealed Classes and Pattern Matching Among other things José Paumard Java Developer Advocate Java Platform Group
  • 3. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 3 Dev.java Java 8 Java 11
  • 4. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 4 Java 17 – LTS! Java 8 Java 11
  • 5. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 5 Java 17 – LTS! Java 8 Java 11 Java 17 ?
  • 6. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 6 Next LTS: Java 21 Java 8 Java 11 Java 17 Java 21
  • 7. Language Type inference for locals (var) Switch expressions Text blocks Record classes Sealed classes Pattern matching for instanceof Compact String, Indify Concatenation JDK 17: New Features Since the JDK 8 Copyright © 2021, Oracle and/or its affiliates 7 Tools jshell jlink jdeps jpackage java source code launcher javadoc search + API history JVM Garbage Collectors: G1, ZGC AArch64 support: Windows, Mac, Linux Docker awareness Class Data Sharing by default Helpful NullPointerExceptions Hidden classes Libraries HTTP client Collection factories Unix-domain sockets Stack walker Deserialization filtering Pseudo-RNG, SHA-3, TLS 1.3
  • 8. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 8 Stop doing that!
  • 9. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 9 Do not call new Integer(...) Stop Calling Wrapper Classes Constructors! @Deprecated(since="9", forRemoval = true) public Integer(int value) { this.value = value; } @IntrinsicCandidate public static Integer valueOf(int i) { // some code return new Integer(i); }
  • 10. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 10 Do not override finalize() Stop Overriding Finalize! @Deprecated(since="9") protected void finalize() throws Throwable { }
  • 11. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 11 Records
  • 12. ?Record and Array Pattern Matching? Record Sealed Classes Switch Expression Constant Dynamic Inner Classes private in VM Nestmates Pattern Matching for instanceof 11 14 16 17 Switch on Patterns 19
  • 13. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 13 In reference to the Amber Chronicles by Roger Zelazny Project Amber
  • 14. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 14 Record Deconstruction if (o instanceof Rectangle rectangle) { int width = rectangle.width(); int height = rectangle.height(); // do something with width and height }
  • 15. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 15 This is record deconstruction width are height are binding variables Record Deconstruction if (o instanceof Rectangle(int width, int height)) { // do something with width and height }
  • 16. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 16 Three type patterns Three pattern binding variables One target operand Pattern Matching for Switch String formatted = switch(number) { case Integer i -> String.format("int %d", i); case Long l -> String.format("long %d", l); case Double d -> String.format("double %d", d); }
  • 17. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 17 Pattern Matching for Switch record Square(int edge) {} record Circle(int radius) {} double area = switch(shape) { case Square(int edge) -> edge* edge; case Circle(int radius) -> Math.PI*radius*radius; default -> ...; }
  • 18. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 18 Array Pattern Matching if (o instanceof String[] array && array.length() >= 2) { // do something with array[0] and array[1] }
  • 19. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 19 Array Pattern Matching if (o instanceof String[] {String s1, String s2}) { // do something with s1 and s2 }
  • 20. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 20 Array Pattern Matching if (o instanceof Circle[] {Circle(var r1), Circle(var r3)}) { // do something with r1 and r2 }
  • 21. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 21 You can use var in patterns Syntaxic Sugars if (shape instanceof Circle(var center, var radius)) { // center and radius are binding variables } record Point(int x, int y) {} record Circle(Point center, int radius) implements Shape {}
  • 22. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 22 You can tell that you do not need a binding variable Syntaxic Sugars if (shape instanceof Circle(var center, _)) { // center and radius are binding variables } record Point(int x, int y) {} record Circle(Point center, int radius) implements Shape {}
  • 23. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 23 You can nest patterns (nested patterns) Syntaxic Sugars if (shape instanceof Circle(var center, _) && center instanceof Point(int x, int y)) { // center and radius are binding variables } if (shape instanceof Circle(Point(int x, int y), _)) { // center and radius are binding variables }
  • 24. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 24 The deconstruction uses the canonical constructor of a record What about: - factory methods? - classes that are not records? Deconstruction
  • 25. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 25 Deconstruction Using Factory Methods interface Shape { static Circle circle(double radius) { return new Circle(radius); } static Square square(double edge) { return new Square(edge); } } record Circle(double radius) {} record Square(double edge) {}
  • 26. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 26 Then this code becomes possible: Deconstruction Using Factory Methods double area = switch(shape) { case Shape.circle(double radius) -> Math.PI*radius*radius; case Shape.square(double edge) -> edge*edge; }
  • 27. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 27 What About Your POJOs? public class Point { private int x, y; public Point(int x, int y) { this.x = x; this.y = y; } public deconstructor(int x, int y) { x = this.x; y = this.y; } } The binding variables are the same external state description Allows defensive copy and overloading
  • 28. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 28 You saw patterns with instanceof and switch Let us see match ! Pattern with Match record Point(int x, int y) {} record Circle(Point center, int radius) implements Shape {} Circle circle = ...; match Circle(var center, var radius) = circle; // center and radius are binding variables
  • 29. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 29 If shape is in fact a rectangle… You can throw an exception Pattern with Match Shape shape = ...; match Circle(var center, var radius) = shape else throw new IllegalStateException("Not a circle"); // center and radius are binding variables
  • 30. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 30 If shape is in fact a rectangle … Or define default values Pattern with Match Shape shape = ...; match Circle(Point center, int radius) = shape else { center = new Point(0, 0); // this is called radius = 1d; // an anonymous matcher } // center and radius are binding variables
  • 31. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 31 You can use match with more than one pattern… … or use nested patterns Pattern with Match Shape shape = ...; match Rectangle(var p1, var p2) = shape, Point(var x0, var y0) = p1, Point(var x1, var y2) = p2;
  • 32. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 32 With factory methods More Examples if (opt instanceof Optional.of(var max)) { // max is a binding variable }
  • 33. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 33 With factory methods More Examples if (s instanceof String.format("%s is %d years old", String name, Integer.valueOf(int age) { // name and age are binding variables }
  • 34. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 34 You can create maps with factory methods This is an extended form of Pattern Matching where you check the value of a binding variable More Examples if (map instanceof Map.withMapping("name", var name) && map instanceof Map.withMapping("email", var email)) { // name and email are binding variables }
  • 35. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 35 Pattern combination More Examples if (map instanceof Map.withMapping("name", var name) __AND map instanceof Map.withMapping("email", var email)) { // name and email are binding variables } __AND = pattern combination
  • 36. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 36 More Examples { "firstName": "John", "lastName": "Smith", "age": 25, "address" : { "street": "21 2nd Street", "city": "New York", "state": "NY", "postalCode": "10021" } } if (json instanceof stringKey("firstName", var firstName) __AND stringKey("lastName", var lastName) __AND intKey("age", var age) __AND objectKey("address", stringKey("stree", var street) __AND stringKey("city", var city) __AND stringKey("state", var state) )) { // firstName, lastName, age, // street, city, state, ... // are binding variables }
  • 37. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 37 If Java Embraces « Map Literals » Map<String, String> map = { "firstName": "John", "lastName": "Smith", "age": "25" } if (map instanceof { "firstName": var firstName, "lastName": var lastName, "age": Integer.toString(var age) }) { // firstName, lastName, age // are binding variables }
  • 38. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 38 • Constant Patterns: checks the operand with a constant value • Type Patterns: checks if the operand has the right type, casts it, and creates a binding variable Patterns at a Glance
  • 39. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 39 • Patterns + Deconstruction: checks the operand type, casts it, bind the component to binding variables • Patterns + Method: uses a factory method or a deconstructor • Patterns + Var: infers the right type, and creates the binding variable • Pattern + _: infers the right type, but does not create the binding variable Patterns at a Glance
  • 40. 3/11/2022 Copyright © 2021, Oracle and/or its affiliates | 40 Where are we? • Pattern Matching for instanceof • Pattern Matching for Switch • Record and Array Pattern Matching • Match • Literals Patterns at a Glance