Slide 1

Slide 1 text

From Java 11 to Java 17 and Beyond Among other things José Paumard @JosePaumard Java Developer Advocate Java Platform Group

Slide 2

Slide 2 text

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.java

Slide 3

Slide 3 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 3 https://dev.java/

Slide 4

Slide 4 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 4 https://dev.java/community/

Slide 5

Slide 5 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 5 https://inside.java/

Slide 6

Slide 6 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 6 Java 17 – LTS! Java 8 Java 11

Slide 7

Slide 7 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 7 Java 17 – LTS! Java 8 Java 11 Java 17 ?

Slide 8

Slide 8 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 8 Next LTS: Java 21 Java 8 Java 11 Java 17 Java 21

Slide 9

Slide 9 text

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 9 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

Slide 10

Slide 10 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 10 Stop doing that!

Slide 11

Slide 11 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 11 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); }

Slide 12

Slide 12 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 12 Do not override finalize() Stop Overriding Finalize! @Deprecated(since="9", forRemoval=true) protected void finalize() throws Throwable { }

Slide 13

Slide 13 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 13 Strings

Slide 14

Slide 14 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 14 A new way of writing Strings Text Blocks String message = """ Hello world!""";

Slide 15

Slide 15 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 15 String Templates (prev.)

Slide 16

Slide 16 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 16 Interpolating strings from a template and variables String Templates (prev.) String name = "world"; String message = STR."Hello \{name}!"; System.out.println(message); > Hello world!

Slide 17

Slide 17 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 17 Is a String Template Expression STR is a template policy "Hello \{name}!" is a template with an embedded expression The evaluation produces a String, but not always String Template Expression String message = STR."Hello \{name}!";

Slide 18

Slide 18 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 18 Embedded expressions are expressions String Template Expression int x = 10, y = 20; String result = STR."\{x} + \{y} = \{x + y}"; System.out.println(result); > 10 + 20 = 30

Slide 19

Slide 19 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 19 Embedded expressions are expressions String Template Expression String result = STR."We have a \{getOfferType()} for you"; System.out.println(result); > We have a gift for you

Slide 20

Slide 20 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 20 Embedded expressions are expressions String Template Expression String time = STR."The time is \{ // The java.time.format package is very useful DateTimeFormatter .ofPattern("HH:mm:ss") .format(LocalTime.now()) } right now"; System.out.println(time); > The time is 12:34:56 right now

Slide 21

Slide 21 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 21 Embedded expressions are expressions String Template Expression int index = 10; String data = STR."\{index++} \{index++} \{index++}"; System.out.println(data); > 10 11 12

Slide 22

Slide 22 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 22 FMTR: formatter template policy More Template Policies String form = FMTR.""" Desc Unit Qty Amount %-10s\{description} $%5.2f\{price} %5d\{quantity} $%5.2f\{price * quantity} Subtotal $%5.2f\{price * quantity} Tax $%5.2f\{price * quantity * tax} Total $%5.2f\{price * quantity * (1.0 + tax)} """; System.out.println(form);

Slide 23

Slide 23 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 23 FMTR: formatter template policy More Template Policies Desc Unit Qty Amount hammer $ 7.88 3 $23.64 Subtotal $23.64 Tax $ 3.55 Total $27.19

Slide 24

Slide 24 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 24 Yes! A policy is a class that implements TemplatePolicy It supports validation to prevent injection It can return any object User Defined Policies?

Slide 25

Slide 25 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 25 Records

Slide 26

Slide 26 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 26 Short answer: a class! Less short answer: a class - That extends the Record class - That is final What is a Record?

Slide 27

Slide 27 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 27 And you can write: I Need to Write a Record! public record User(String name, int age) { } User sarah = new User("Sarah", 23); System.out.println(sarah); $ User[name=Sarah, age=23]

Slide 28

Slide 28 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 28 1) A constructor 2) Accessors: name() and age() 3) The following methods: toString(), equals(), hashCode() What Do you Get with a Record? public record User(String name, int age) { }

Slide 29

Slide 29 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 29 You cannot extend a record A record cannot extend anything You cannot create any instance field in a record You can implement interfaces You can add any instance method You can add any static field or method Restrictions on Records

Slide 30

Slide 30 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 30 name and age are the components of the record Components are both final instance fields And accessors Components of a Record public record User(String name, int age) { }

Slide 31

Slide 31 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 31 More on Components public record User(String name, int age) { } User user = new User("Sarah", 23); Class userClass = User.class; boolean isRecord = userClass.isRecord(); Component[] components = userClass.getRecordComponents();

Slide 32

Slide 32 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 32 More on Components public record User(String name, int age) { } User user = new User("Sarah", 23); RecordComponent nameComponent = components[0]; Class> declaring = nameComponent.getDeclaringRecord(); String name = (String) nameComponent.getAccessor()invoke(user);

Slide 33

Slide 33 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 33 More on Components public record User(@UserName String name, int age) { } @Target(ElementType.RECORD_COMPONENT) @interface UserName {}

Slide 34

Slide 34 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 34 The state of a record is entirely defined by its components - You cannot add more state by extension, or by extending anything - You cannot add any instance field Record State

Slide 35

Slide 35 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 35 The constructor of a record that takes its components is called the canonical constructor Record Building public record User(String name, int age) { public record User(String name, int age) { this.name = name; this.age = age; } }

Slide 36

Slide 36 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 36 Why would you write your own canonical constructor? 1) Defensive copy Record Building public record State(List cities) { public State(List cities) { this.cities = List.copyOf(cities); } }

Slide 37

Slide 37 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 37 In that case, you need to write your own accessor too! Record Building public record State(List cities) { public State(List cities) { this.cities = List.copyOf(cities); } public List cities() { return List.copyOf(cities); } }

Slide 38

Slide 38 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 38 Why would you write your own canonical constructor? 2) Validation Record Building public record Range(int begin, int end) { public Range(int begin, int end) { if (begin > end) { throw new IllegalArgumentException("..."); } this.begin = begin; this.end = end; } }

Slide 39

Slide 39 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 39 Why would you write your own canonical constructor? 2) Validation, with the Compact Constructor Record Building public record Range(int begin, int end) { public Range { if (begin > end) { throw new IllegalArgumentException("..."); } } }

Slide 40

Slide 40 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 40 A record constructor must call the canonical constructor Record Building public record Range(int begin, int end) { public Range(int end) { this.begin = 0; this.end = end; } }

Slide 41

Slide 41 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 41 A record constructor must call the canonical constructor Record Building public record Range(int begin, int end) { public Range(int end) { this(0, end); } }

Slide 42

Slide 42 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 42 You cannot build a record without calling its canonical constructor Deserialization calls the canonical constructor of a record

Slide 43

Slide 43 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 43 Deserialization calls the canonical constructor of a record Records are a perfect choice for Data Transport Objects Record Serialization

Slide 44

Slide 44 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 44 Sealed Types

Slide 45

Slide 45 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 45 Short answer: type (interface, class, abstract class)! Less short answer: a type - That declares its subtypes - That lives in the same package or module as them What is a Sealed Type?

Slide 46

Slide 46 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 46 A sealed type must declare its extension Syntax public sealed interface Shape permits Square, Rectangle, WeirdShape { }

Slide 47

Slide 47 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 47 An extension of a sealed type must be: - final - or sealed - or non-sealed Syntax

Slide 48

Slide 48 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 48 An extension of a sealed type must be: - final Syntax public record Square(int edge) implements Shape { }

Slide 49

Slide 49 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 49 An extension of a sealed type must be: - sealed Syntax public sealed class Rectangle permits ExtendedRectangle { }

Slide 50

Slide 50 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 50 An extension of a sealed type must be: - non-sealed Syntax public non-sealed abstract class WeirdShape { }

Slide 51

Slide 51 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 51 Pattern Matching

Slide 52

Slide 52 text

?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 Types 19

Slide 53

Slide 53 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 53 In reference to the Amber Chronicles by Roger Zelazny Project Amber

Slide 54

Slide 54 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 54 A pattern is a combination of a match predicate that determines if the pattern matches a target, along with a set of pattern variables that are conditionally extracted if the pattern matches the target. Java 16 : Pattern Match for IntanceOf Pattern Matching for Java Gavin Bierman and Brian Goetz, September 2018 https://github.com/openjdk/amber-docs/blob/master/site/design-notes/pattern-matching-for-java.md

Slide 55

Slide 55 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 55 String name = the type of the pattern String = the name of the pattern name = the binding variable o = target operand Type Pattern if (o instanceof String name) { // some code }

Slide 56

Slide 56 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 56 Type Pattern if (o instanceof String name) { Sytem.out.println(name.toUpperCase()); } if (o instanceof String name && name.length() > 0) { Sytem.out.println(name.toUpperCase()); }

Slide 57

Slide 57 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 57 Type Pattern if (o instanceof String name) { Sytem.out.println(name.toUpperCase()); } if (!(o instanceof String name)) { // you cant use name here! return; // or throw an exception } Sytem.out.println(name.toUpperCase());

Slide 58

Slide 58 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 58 Record Pattern Matching (prev Java 19?)

Slide 59

Slide 59 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 59 Record Pattern Matching record User(String name, int age) {} if (o instanceof User user)) { String name = user.name(); int age = user.age(); }

Slide 60

Slide 60 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 60 Record Pattern Matching record User(String name, int age) {} if (o instanceof User(String name, int age))) { // use name and age }

Slide 61

Slide 61 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 61 Record Pattern Matching record User(String name, int age) {} if (o instanceof User(String name, int age) user)) { // use name and age }

Slide 62

Slide 62 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 62 Record deconstruction is easy because a record is built on components And components are declared along with it The binding variables are initialized by calling the accessors Deconstructing a Record

Slide 63

Slide 63 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 63 Array Pattern Matching

Slide 64

Slide 64 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 64 Array Pattern Matching if (o instanceof String[] array && array.length() >= 2) { // do something with array[0] and array[1] }

Slide 65

Slide 65 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 65 Array Pattern Matching if (o instanceof String[] {s1, s2}) { // do something with s1 and s2 }

Slide 66

Slide 66 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 66 Nesting Patterns + Type Inference if (o instanceof Circle[] {Circle(var r1), Circle(var r2)}) { // do something with s1 and s2 } record Circle(int radius) {}

Slide 67

Slide 67 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 67 Nesting Patterns + Type Inference if (o instanceof Circle[] { Circle(Point(int x1, int y1), _), Circle(Point(int x2, int y2), _)}) { // do something with x1, y1, x2, y2 } record Circle(Point p1, int radius) {} record Point(int x, int y) {}

Slide 68

Slide 68 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 68 Pattern Matching for Switch

Slide 69

Slide 69 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 69 Pattern Matching for Switch interface Shape {} record Square(int edge) implements Shape {} record Circle(int radius) implements Shape {}

Slide 70

Slide 70 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 70 Pattern Matching for Switch double area(Shape shape) { return switch (shape) { case Square square -> square.edge()*square.edge(); case Circle circle -> Math.PI*circle.radius()* circle.radius(); default -> 0; } }

Slide 71

Slide 71 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 71 Switch on a Sealed Type sealed interface Shape permits Square, Circle {} record Square(int edge) implements Shape {} record Circle(int radius) implements Shape {}

Slide 72

Slide 72 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 72 Switch on a Sealed Type double area(Shape shape) { return switch (shape) { case Square square -> square.edge()*square.edge(); case Circle circle -> Math.PI*circle.radius()* circle.radius(); // default -> 0; // Not needed! } }

Slide 73

Slide 73 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 73 The compiler can: - check if a type is not present and raise an error - raise a warning if an uneeded default is present Switch on a Sealed Type

Slide 74

Slide 74 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 74 Guarded Pattern (prev. 17 and 18) double area(Shape shape) { return switch (shape) { case Square square && square.edge() == 0 -> 0; case Square square -> square.edge()*square.edge(); case Circle circle -> Math.PI*circle.radius()* circle.radius(); } }

Slide 75

Slide 75 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 75 Guarded Pattern (prev. 17 and 18) double area(Shape shape) { return switch (shape) { case Square square && square.edge() == 0 -> 0; case Square square -> square.edge()*square.edge(); case Circle circle -> Math.PI*circle.radius()* circle.radius(); } }

Slide 76

Slide 76 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 76 Switch Pattern with When (prev. 19?) double area(Shape shape) { return switch (shape) { case Square square when square.edge() == 0 -> 0; case Square square -> square.edge()*square.edge(); case Circle circle -> Math.PI*circle.radius()* circle.radius(); } }

Slide 77

Slide 77 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 77 (19 prev?) Switch and Record Pattern Matching double area(Shape shape) { return switch (shape) { case Square(int edge) when edge == 0 -> 0; case Square(int edge) -> edge*edge; case Circle(int radius) -> Math.PI*radius*radius; } }

Slide 78

Slide 78 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 78 The Future of Pattern Matching

Slide 79

Slide 79 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 79 Nested Patterns, needed binding variables, type inference 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 }

Slide 80

Slide 80 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 80 The deconstruction uses the canonical constructor of a record What about: - factory methods? Deconstruction

Slide 81

Slide 81 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 81 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) {}

Slide 82

Slide 82 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 82 Then this code could become 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; }

Slide 83

Slide 83 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 83 With factory methods Deconstruction Using Factory Methods if (opt instanceof Optional.of(var max)) { // max is a binding variable }

Slide 84

Slide 84 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 84 With factory methods Deconstruction Using Factory Methods if (s instanceof String.format("%s is %d years old", String name, Integer.valueOf(int age) { // name and age are binding variables }

Slide 85

Slide 85 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 85 Map deconstruction This is an extended form of Pattern Matching where you check the value of a binding variable Deconstruction Using Factory Methods if (map instanceof Map.withMapping("name", var name) && map instanceof Map.withMapping("email", var email)) { // name and email are binding variables }

Slide 86

Slide 86 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 86 Pattern combination More Examples if (map instanceof Map.withMapping("name", var name) __AND Map.withMapping("email", var email)) { // name and email are binding variables } __AND = pattern combination

Slide 87

Slide 87 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 87 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("street", var street) __AND stringKey("city", var city) __AND stringKey("state", var state) )) { // firstName, lastName, age, // street, city, state, ... // are binding variables }

Slide 88

Slide 88 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 88 If Java Embraces « Map Literals » Map 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 // age is a deconstructed int }

Slide 89

Slide 89 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 89 The deconstruction uses the canonical constructor of a record What about: - factory methods? - classes that are not records? Deconstruction

Slide 90

Slide 90 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 90 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; } } This is the output of the deconstructor It allows both: - overloading - defensive copy

Slide 91

Slide 91 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 91 Match can check if an expression matches a pattern Just as if checks for a boolean expression Introducing Match

Slide 92

Slide 92 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 92 Pattern with Match (or Let?) 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

Slide 93

Slide 93 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 93 Pattern with Let record Point(int x, int y) {} record Circle(Point center, int radius) implements Shape {} Circle circle = ...; let Circle(var center, var radius) = circle else throw new IllegalStateException("Not a circle");

Slide 94

Slide 94 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 94 Pattern with Let record Point(int x, int y) {} record Circle(Point center, int radius) implements Shape {} Circle circle = ...; let Circle(var center, var radius) = circle else { center = new Point(0, 0); // this is called radius = 0; // an anonymous matcher }

Slide 95

Slide 95 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 95 You can use match with more than one pattern… … or use nested patterns Pattern with Let Shape shape = ...; let Rectangle(var p1, var p2) = shape, Point(var x0, var y0) = p1, Point(var x1, var y2) = p2;

Slide 96

Slide 96 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 96 Nesting Patterns + Type Inference List points = ...; for (Point point: points) { int x = point.x(); int y = point.x(); // do something with x and y } record Point(int x, int y) {}

Slide 97

Slide 97 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 97 Nesting Patterns + Type Inference List points = ...; for (Point(int x, int y): points) { // do something with x and y } record Point(int x, int y) {}

Slide 98

Slide 98 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 98 • 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

Slide 99

Slide 99 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 99 • 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

Slide 100

Slide 100 text

5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 100 Where are we? • Pattern Matching for instanceof (16) • Pattern Matching for Switch (prev 17, 18, 19?) • Record and Array Pattern Matching (19?) • Match • Literals Patterns at a Glance