Weitere ähnliche Inhalte Ähnlich wie From Java 11 to 17 and beyond.pdf (20) Mehr von José Paumard (20) Kürzlich hochgeladen (20) From Java 11 to 17 and beyond.pdf1. From Java 11 to Java 17 and Beyond
Among other things
José Paumard
@JosePaumard
Java Developer Advocate
Java Platform Group
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 {
}
14. 5/1/2022
Copyright © 2021, Oracle and/or its affiliates |
14
A new way of writing Strings
Text Blocks
String message = """
Hello world!""";
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?
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
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