SlideShare ist ein Scribd-Unternehmen logo
1 von 100
Downloaden Sie, um offline zu lesen
From Java 11 to Java 17 and Beyond
Among other things
José Paumard
@JosePaumard
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.java
5/1/2022
Copyright © 2021, Oracle and/or its affiliates |
3
https://dev.java/
5/1/2022
Copyright © 2021, Oracle and/or its affiliates |
4
https://dev.java/community/
5/1/2022
Copyright © 2021, Oracle and/or its affiliates |
5
https://inside.java/
5/1/2022
Copyright © 2021, Oracle and/or its affiliates |
6
Java 17 – LTS!
Java 8 Java 11
5/1/2022
Copyright © 2021, Oracle and/or its affiliates |
7
Java 17 – LTS!
Java 8
Java 11
Java 17 ?
5/1/2022
Copyright © 2021, Oracle and/or its affiliates |
8
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
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
5/1/2022
Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted
10
Stop doing that!
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);
}
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 {
}
5/1/2022
Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted
13
Strings
5/1/2022
Copyright © 2021, Oracle and/or its affiliates |
14
A new way of writing Strings
Text Blocks
String message = """
Hello world!""";
5/1/2022
Copyright © 2021, Oracle and/or its affiliates |
15
String Templates (prev.)
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!
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}!";
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
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
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
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
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);
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
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?
5/1/2022
Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted
25
Records
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?
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]
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) { }
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
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) { }
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<User> userClass = User.class;
boolean isRecord = userClass.isRecord();
Component[] components = userClass.getRecordComponents();
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);
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 {}
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
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;
}
}
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<City> cities) {
public State(List<City> cities) {
this.cities = List.copyOf(cities);
}
}
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<City> cities) {
public State(List<City> cities) {
this.cities = List.copyOf(cities);
}
public List<City> cities() {
return List.copyOf(cities);
}
}
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;
}
}
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("...");
}
}
}
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;
}
}
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);
}
}
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
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
5/1/2022
Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted
44
Sealed Types
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?
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 {
}
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
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 { }
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 { }
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
{ }
5/1/2022
Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted
51
Pattern Matching
?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
5/1/2022
Copyright © 2021, Oracle and/or its affiliates |
53
In reference to the Amber Chronicles
by Roger Zelazny
Project Amber
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
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
}
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());
}
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());
5/1/2022
Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted
58
Record Pattern Matching
(prev Java 19?)
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();
}
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
}
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
}
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
5/1/2022
Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted
63
Array Pattern Matching
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]
}
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
}
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) {}
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) {}
5/1/2022
Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted
68
Pattern Matching for Switch
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 {}
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;
}
}
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 {}
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!
}
}
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
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();
}
}
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();
}
}
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();
}
}
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;
}
}
5/1/2022
Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted
78
The Future of Pattern Matching
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
}
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
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) {}
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;
}
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
}
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
}
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
}
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
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
}
5/1/2022
Copyright © 2021, Oracle and/or its affiliates |
88
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
// age is a deconstructed int
}
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
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
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
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
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");
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
}
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;
5/1/2022
Copyright © 2021, Oracle and/or its affiliates |
96
Nesting Patterns + Type Inference
List<Point> 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) {}
5/1/2022
Copyright © 2021, Oracle and/or its affiliates |
97
Nesting Patterns + Type Inference
List<Point> points = ...;
for (Point(int x, int y): points) {
// do something with x and y
}
record Point(int x, int y) {}
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
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
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

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!
Jakub Kubrynski
 

Was ist angesagt? (20)

Spring Core
Spring CoreSpring Core
Spring Core
 
Spring boot - an introduction
Spring boot - an introductionSpring boot - an introduction
Spring boot - an introduction
 
Introduction to Spring Boot
Introduction to Spring BootIntroduction to Spring Boot
Introduction to Spring Boot
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Spring boot introduction
Spring boot introductionSpring boot introduction
Spring boot introduction
 
Introduction to Spring Boot!
Introduction to Spring Boot!Introduction to Spring Boot!
Introduction to Spring Boot!
 
Spring boot
Spring bootSpring boot
Spring boot
 
Spring Boot
Spring BootSpring Boot
Spring Boot
 
Introduction to spring boot
Introduction to spring bootIntroduction to spring boot
Introduction to spring boot
 
Spring framework aop
Spring framework aopSpring framework aop
Spring framework aop
 
Spring boot
Spring bootSpring boot
Spring boot
 
Spring boot jpa
Spring boot jpaSpring boot jpa
Spring boot jpa
 
LatJUG. Java Bytecode Fundamentals
LatJUG. Java Bytecode FundamentalsLatJUG. Java Bytecode Fundamentals
LatJUG. Java Bytecode Fundamentals
 
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
 
Nestjs MasterClass Slides
Nestjs MasterClass SlidesNestjs MasterClass Slides
Nestjs MasterClass Slides
 
Java 8-streams-collectors-patterns
Java 8-streams-collectors-patternsJava 8-streams-collectors-patterns
Java 8-streams-collectors-patterns
 
Utilizing kotlin flows in an android application
Utilizing kotlin flows in an android applicationUtilizing kotlin flows in an android application
Utilizing kotlin flows in an android application
 
Spring Boot & Actuators
Spring Boot & ActuatorsSpring Boot & Actuators
Spring Boot & Actuators
 
PUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBootPUC SE Day 2019 - SpringBoot
PUC SE Day 2019 - SpringBoot
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
 

Ähnlich wie From Java 11 to 17 and beyond.pdf

4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf
amitbhachne
 
Daniel Egan Msdn Tech Days Oc Day2
Daniel Egan Msdn Tech Days Oc Day2Daniel Egan Msdn Tech Days Oc Day2
Daniel Egan Msdn Tech Days Oc Day2
Daniel Egan
 

Ähnlich wie From Java 11 to 17 and beyond.pdf (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
 
The Future of Java: Records, Sealed Classes and Pattern Matching
The Future of Java: Records, Sealed Classes and Pattern MatchingThe Future of Java: Records, Sealed Classes and Pattern Matching
The Future of Java: Records, Sealed Classes and Pattern Matching
 
Overview of entity framework by software outsourcing company india
Overview of entity framework by software outsourcing company indiaOverview of entity framework by software outsourcing company india
Overview of entity framework by software outsourcing company india
 
C#Portfolio
C#PortfolioC#Portfolio
C#Portfolio
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
Intro to JavaScript for APEX Developers
Intro to JavaScript for APEX DevelopersIntro to JavaScript for APEX Developers
Intro to JavaScript for APEX Developers
 
Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2Design Patterns - Part 1 of 2
Design Patterns - Part 1 of 2
 
Refactoring Workflows & Techniques Presentation by Valentin Stantescu
Refactoring Workflows & Techniques Presentation by Valentin StantescuRefactoring Workflows & Techniques Presentation by Valentin Stantescu
Refactoring Workflows & Techniques Presentation by Valentin Stantescu
 
"Quantum" Performance Effects
"Quantum" Performance Effects"Quantum" Performance Effects
"Quantum" Performance Effects
 
Java Cloud and Container Ready
Java Cloud and Container ReadyJava Cloud and Container Ready
Java Cloud and Container Ready
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Getting start Java EE Action-Based MVC with Thymeleaf
Getting start Java EE Action-Based MVC with ThymeleafGetting start Java EE Action-Based MVC with Thymeleaf
Getting start Java EE Action-Based MVC with Thymeleaf
 
Salesforce meetup | Lightning Web Component
Salesforce meetup | Lightning Web ComponentSalesforce meetup | Lightning Web Component
Salesforce meetup | Lightning Web Component
 
4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf4CS4-25-Java-Lab-Manual.pdf
4CS4-25-Java-Lab-Manual.pdf
 
Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)Back-2-Basics: .NET Coding Standards For The Real World (2011)
Back-2-Basics: .NET Coding Standards For The Real World (2011)
 
Lambdas & Streams
Lambdas & StreamsLambdas & Streams
Lambdas & Streams
 
Presentation on design pattern software project lll
 Presentation on design pattern  software project lll  Presentation on design pattern  software project lll
Presentation on design pattern software project lll
 
Completable future
Completable futureCompletable future
Completable future
 
Whats new in ES2019
Whats new in ES2019Whats new in ES2019
Whats new in ES2019
 
Daniel Egan Msdn Tech Days Oc Day2
Daniel Egan Msdn Tech Days Oc Day2Daniel Egan Msdn Tech Days Oc Day2
Daniel Egan Msdn Tech Days Oc Day2
 

Mehr von José Paumard

Mehr von José Paumard (20)

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!
 
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
 
ArrayList et LinkedList sont dans un bateau
ArrayList et LinkedList sont dans un bateauArrayList et LinkedList sont dans un bateau
ArrayList et LinkedList sont dans un bateau
 
Java SE 8 for Java EE developers
Java SE 8 for Java EE developersJava SE 8 for Java EE developers
Java SE 8 for Java EE developers
 
Asynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFutureAsynchronous API in Java8, how to use CompletableFuture
Asynchronous API in Java8, how to use CompletableFuture
 
Java 8 Streams and Rx Java Comparison
Java 8 Streams and Rx Java ComparisonJava 8 Streams and Rx Java Comparison
Java 8 Streams and Rx Java Comparison
 

Kürzlich hochgeladen

Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Kürzlich hochgeladen (20)

Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 

From Java 11 to 17 and beyond.pdf

  • 1. From Java 11 to Java 17 and Beyond Among other things José Paumard @JosePaumard Java Developer Advocate Java Platform Group
  • 3. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 3 https://dev.java/
  • 4. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 4 https://dev.java/community/
  • 5. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 5 https://inside.java/
  • 6. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 6 Java 17 – LTS! Java 8 Java 11
  • 7. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 7 Java 17 – LTS! Java 8 Java 11 Java 17 ?
  • 8. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 8 Next LTS: Java 21 Java 8 Java 11 Java 17 Java 21
  • 9. 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
  • 10. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 10 Stop doing that!
  • 11. 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); }
  • 12. 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 { }
  • 13. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 13 Strings
  • 14. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 14 A new way of writing Strings Text Blocks String message = """ Hello world!""";
  • 15. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 15 String Templates (prev.)
  • 16. 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!
  • 17. 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}!";
  • 18. 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
  • 19. 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
  • 20. 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
  • 21. 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
  • 22. 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);
  • 23. 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
  • 24. 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?
  • 25. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 25 Records
  • 26. 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?
  • 27. 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]
  • 28. 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) { }
  • 29. 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
  • 30. 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) { }
  • 31. 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<User> userClass = User.class; boolean isRecord = userClass.isRecord(); Component[] components = userClass.getRecordComponents();
  • 32. 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);
  • 33. 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 {}
  • 34. 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
  • 35. 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; } }
  • 36. 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<City> cities) { public State(List<City> cities) { this.cities = List.copyOf(cities); } }
  • 37. 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<City> cities) { public State(List<City> cities) { this.cities = List.copyOf(cities); } public List<City> cities() { return List.copyOf(cities); } }
  • 38. 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; } }
  • 39. 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("..."); } } }
  • 40. 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; } }
  • 41. 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); } }
  • 42. 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
  • 43. 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
  • 44. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 44 Sealed Types
  • 45. 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?
  • 46. 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 { }
  • 47. 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
  • 48. 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 { }
  • 49. 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 { }
  • 50. 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 { }
  • 51. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 51 Pattern Matching
  • 52. ?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
  • 53. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 53 In reference to the Amber Chronicles by Roger Zelazny Project Amber
  • 54. 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
  • 55. 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 }
  • 56. 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()); }
  • 57. 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());
  • 58. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 58 Record Pattern Matching (prev Java 19?)
  • 59. 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(); }
  • 60. 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 }
  • 61. 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 }
  • 62. 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
  • 63. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 63 Array Pattern Matching
  • 64. 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] }
  • 65. 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 }
  • 66. 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) {}
  • 67. 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) {}
  • 68. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 68 Pattern Matching for Switch
  • 69. 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 {}
  • 70. 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; } }
  • 71. 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 {}
  • 72. 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! } }
  • 73. 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
  • 74. 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(); } }
  • 75. 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(); } }
  • 76. 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(); } }
  • 77. 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; } }
  • 78. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | Confidential: Internal/Restricted/Highly Restricted 78 The Future of Pattern Matching
  • 79. 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 }
  • 80. 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
  • 81. 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) {}
  • 82. 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; }
  • 83. 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 }
  • 84. 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 }
  • 85. 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 }
  • 86. 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
  • 87. 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 }
  • 88. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 88 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 // age is a deconstructed int }
  • 89. 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
  • 90. 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
  • 91. 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
  • 92. 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
  • 93. 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");
  • 94. 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 }
  • 95. 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;
  • 96. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 96 Nesting Patterns + Type Inference List<Point> 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) {}
  • 97. 5/1/2022 Copyright © 2021, Oracle and/or its affiliates | 97 Nesting Patterns + Type Inference List<Point> points = ...; for (Point(int x, int y): points) { // do something with x and y } record Point(int x, int y) {}
  • 98. 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
  • 99. 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
  • 100. 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