SlideShare a Scribd company logo
1 of 43
The Art of Java
Type Patterns
Simon Ritter, Deputy CTO | Azul
2
Modern Java
• The six-month release cadence for Java has been really good
• Lots of new features added much faster than we've ever seen before
• Significant language changes are initially developed under project Amber
o "... explore and incubate smaller, productivity-oriented Java language features..."
o Most features go through at least two rounds of preview
• Many of the new features work both separately and combined with others
3
Pattern Matching In Java
• java.util.regex
• This is not what we're here to talk about
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");
boolean b = m.matches();
boolean b = Pattern.matches("a*b", "aaaaab");
4
Pattern Matching Fundamentals
• A well used technique, been in use since the 1960s (used in Haskell, AWK, etc.)
match predicate pattern variables
Determines whether the pattern
matches a target
A pattern
Conditionally extracted if the
pattern matches the target
5
Pattern Types
• Constant
o Match on a constant (already in use in a switch statement)
• Type
o Match on a type
• Deconstruction
o Match and extract
• var
o Uses type inference to map to a type pattern (effecively matches anything)
• Any (_)
o Matches anything but binds to nothing (an unused pattern variable). See JEP 302
6
Before we get into to Pattern Matching
7
Switch Expressions
• Switch construct was a statement
o No concept of generating a result that could be assigned
• Rather clunky syntax
o Every case statement needs to be separated
o Must remember break (default is to fall through)
o Scope of local variables is not intuitive
8
Switch Statement
int numberOfLetters;
switch (day) {
case MONDAY:
case FRIDAY:
case SUNDAY:
numberOfLetters = 6;
break;
case TUESDAY:
numberOfLetters = 7;
break;
case THURSDAY:
case SATURDAY:
numberOfLetters = 8;
break;
case WEDNESDAY:
numberOfLetters = 9;
break;
default:
throw new IllegalStateException("Huh?: " + day); };
9
Switch Expression (JDK 12)
• Switch expressions must be complete (exhaustive)
o We'll come back to this later
int numberOfLetters = switch (day) {
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> 9;
default -> throw new IllegalStateException("Huh?: " + day);
};
10
Algebraic Data Types in Java
11
Simple Java Data Class
class Point {
private final double x;
private final double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double x() {
return x;
}
public double y() {
return y;
}
}
12
Records (JDK 14)
record Point(double x, double y) { }
record Anything<T>(T t) { } // Generic Record
public record Circle(double radius) {
private static final double PI = 3.142; // Static instance fields are allowed
public double area() {
return PI * radius * radius;
}
}
13
Records Additional Details
• The base class of all records is java.lang.Record
o Records cannot sub-class (but may implement interfaces)
• Object methods equals(), hashCode() and toString() can be overridden
• Records are implicitly final (although you may add the modifier)
• Records do not follow the Java bean pattern
o x() not getX() in Point example
o record Point(getX, getY) // If you must
14
Java Inheritance
• A class (or interface) in Java can be sub-classed by any class
o Unless it is marked as final
Shape
Triangle Square Pentagon
15
Sealed Classes (JEP 360)
public sealed class Shape permits Triangle, Square, Pentagon { ... }
Shape
Triangle Square Pentagon Circle
X
16
Sealed Classes (JEP 360)
• All sub-classes must have inheritance capabilities explicitly specified
// Restrict sub-classes to defined set
public sealed class Triangle permits Equilateral, Isosoles extends Shape { ... }
// Prevent any further sub-classing
public final class Square extends Shape { ... }
// Allow any classes to sub-class this one (open)
public non-sealed class Pentagon extends Shape { ... }
17
Current Pattern Matching in Java
18
Using The instanceof Operator
if (obj instanceof String) {
String s = (String)obj;
System.out.println(s.length());
}
We must always perform an
explicit cast with an assignment
19
Pattern Matching For instanceof (JDK 14)
if (obj instanceof String s)
System.out.println(s.length());
else
// Use of s not allowed here
if (obj instanceof String s && s.length() > 0)
System.out.println(s.length());
// Compiler error
if (obj instanceof String s || s.length() > 0)
System.out.println(s.length());
20
Pattern Matching For instanceof
public void doSomething(Object o) {
if (!(o instanceof String s))
return;
System.out.println(s.length()); // Scope of s valid
// Several hundred lines of code
System.out.println(s.length()); // Still in scope
}
22
Flow Scoping For Binding Variables
• Scope of local variable runs from its declaration until the end of the block in which it is declared
o Locals are subject to definite assigment
• Binding variables are also subject to definite assignment
o The scope of a binding variable is the set of places in the program where it would be definitely assigned
o This is flow scoping
• However, scope is not the same as local variables
if (o instanceof Integer num) { ... }
else if (o instanceof Float num) { ... }
else if (o instanceof Long num) { ... }
Need flow scoping to be able
to reuse num as variable name
23
Pattern Matching For instanceof Puzzler
• Will this work?
Object s = new Object();
if (s instanceof String s)
System.out.println("String of length " + s.length());
else
System.out.println("No string");
25
Pattern Matching For switch
• Switch is limited on what types you can use (Integral values, Strings, enumerations)
• Expanded to allow type patterns to be matched
void typeTester(Object o) {
switch (o) {
case null -> System.out.println("Null type");
case String s -> System.out.println("String: " + s);
case Color c -> System.out.println("Color with RGB: " + c.getRGB());
case int[] ia -> System.out.println("Array of ints, length" + ia.length);
default -> System.out.println(o.toString());
}
}
26
Pattern Matching For switch
• Null is special (and complicated)
• case null can be used in all switch statements and expressions
o If not included, it will be added by compiler at start (throwing NullPointerException)
void typeTester(Object o) {
switch (o) {
case String s -> System.out.println("String: " + s);
case Color c -> System.out.println("Color with RGB: " + c.getRGB());
case int[] ia -> System.out.println("Array of ints, length" + ia.length);
default -> System.out.println("Bad input!");
}
}
27
Pattern Matching For switch
• Null is special (and complicated)
• case null can be used in all switch statements and expressions
o If not included, it will be added by compiler at start (throwing NullPointerException)
void typeTester(Object o) {
switch (o) {
case null -> throw new NullPointerException(); // Added by compiler
case String s -> System.out.println("String: " + s);
case Color c -> System.out.println("Color with RGB: " + c.getRGB());
case int[] ia -> System.out.println("Array of ints, length" + ia.length);
default -> System.out.println("Bad input!");
}
}
28
Pattern Matching For switch
• Null is special (and complicated)
• case null can be used in all switch statements and expressions
o If not included, it will be added by compiler at start (throwing NullPointerException)
void typeTester(Object o) {
switch (o) {
case String s -> System.out.println("String: " + s);
case Color c -> System.out.println("Color with RGB: " + c.getRGB());
case int[] ia -> System.out.println("Array of ints, length" + ia.length);
null, default -> System.out.println("Bad input!"); // No NullPointerException
}
}
29
Pattern Matching for switch (Completeness)
• Pattern switch statements (and all switch expressions) must be exhaustive
o All possible values must be handled
void typeTester(Object o) {
switch (o) {
case String s -> System.out.println("String: " + s);
case Integer i -> System.out.println("Integer with value " + i.getInteger());
}
}
void typeTester(Object o) {
switch (o) {
case String s -> System.out.println("String: " + s);
case Integer i -> System.out.println("Integer with value " + i.getInteger());
default -> System.out.println("Some other type");
}
}
30
Pattern Matching for switch (Completeness)
void typeTester(Shape shape) { // Using previous sealed class example
switch (shape) {
case Triangle t -> System.out.println("It's a triangle");
case Square s -> System.out.println("It's a square");
case Pentagon p -> System.out.println("It's a pentagon");
case Shape s -> System.out.println("It's a shape");
}
}
31
Guarded Patterns
void shapeTester(Shape shape) { // Using previous sealed class example
switch (shape) {
case Triangle t && t.area() > 25 -> System.out.println("It's a big triangle");
case Triangle t -> System.out.println("It's a small triangle");
case Square s -> System.out.println("It's a square");
case Pentagon p -> System.out.println("It's a pentagon");
case Shape s -> System.out.println("It's a shape");
}
}
GuardedPattern:
PrimaryPattern && ConditionalAndExpression
32
Pattern Dominance
• Less specific cases must not hide more specific cases
void typeTester(Shape shape) {
switch (shape) {
case Shape s -> System.out.println("It's a shape");
case Triangle t -> System.out.println("It's a triangle");
case Square s -> System.out.println("It's a square");
case Pentagon p -> System.out.println("It's a pentagon");
}
}
Shape will always match first
Triangle, Square, Pentagon cases
are unreachable
33
Pattern Dominance
void shapeTester(Shape shape) {
switch (shape) {
case Triangle t -> System.out.println("It's a small triangle");
case Triangle t && t.area() > 25 -> System.out.println("It's a big triangle");
case Square s -> System.out.println("It's a square");
case Pentagon p -> System.out.println("It's a pentagon");
case Shape s -> System.out.println("It's a shape");
}
}
Again, Triangle will match before
Triangle with a guard
34
Pattern Matching in Future Java
35
Pattern Matching instanceof And Records
• What we can do now
• This is good but we can do better
record Point(double x, double y) { }
public void pythagoras(Object o) {
if (o instanceof Point p) {
double x = p.x();
double y = p.y();
System.out.println("Hypotonuse = " + Math.sqrt((x*x) + (y*y));
}
}
36
Pattern Matching For Records (JEP 405)
• Use of a record pattern (which is a deconstruction pattern)
• Since a Record is just a special form of class, this will work with normal classes as well
public void pythagoras(Object o) {
if (o instanceof Point(double x, double y))
System.out.println("Hypotonuse = " + Math.sqrt((x*x) + (y*y));
}
37
Patterns Are Composable
record Point(double x, double y) {};
enum Colour { RED, GREEN, BLUE };
record ColourPoint(Point p, Colour c) {};
record ColourRectangle(ColourPoint topLeft,
ColourPoint bottomRight) implements Shape { ... };
public void printColour(Shape s) {
if (s instanceof ColourRectangle(ColourPoint topleft, ColourPoint bottomRight))
System.out.println(topLeft.c());
}
But this is a record as well
38
Patterns Are Composable
public void printColour(Shape s) {
if (s instanceof ColourRectangle(ColourPoint(Point p, Colour c), ColourPoint br))
System.out.println(c);
}
public void printTopLeftX(Shape s) {
if (s instanceof
ColourRectangle(ColourPoint(Point(double x, double y), Colour c), ColourPoint br)
System.out.println(x);
}
Yet another record
39
Patterns And Local Variable Type Inference
• Use var, introduced in JDK 10
• An example of the any pattern matching type
public void printTopLeftX(Shape s) {
if (s instanceof
ColourRectangle(ColourPoint(Point(var x, var y), var c), var br)
System.out.println(x);
}
40
Using The Any Pattern Match
• Not yet part of any proposed JEP
• Related to JEP 302, Lambda Leftovers
• This is not a proposed change, at this time (i.e. I'm speculating this as a feature)
public void printTopLeftX(Shape s) {
if (s instanceof
ColourRectangle(ColourPoint(Point(var x, _), _), _)
System.out.println(x);
}
We have no interest in these
variables so let's ignore them
41
Pattern Matching For Arrays
• Why not use a decompose pattern for arrays?
• This was part of JEP 405 but has been dropped for now
static void printFirstTwoStrings(Object o) {
if (o instanceof String[] sa && sa.length >= 2) {
String s1 = sa[0];
String s2 = sa[1];
System.out.println(s1 + s2);
}
}
static void printFirstTwoStrings(Object o) {
if (o instanceof String[] { String s1, String s2, ... })
System.out.println(s1 + s2);
}
Summary
43
Conclusions
• Pattern matching is a powerful set of language constructs
• Simplifies certain tasks
o Less boilerplate code
o More declarative
• Also has the potential for better optimisation of code and enhanced performance
• Some features already included
• More to come
• Yet more may be added later
44
Azul Zulu Builds of OpenJDK
• Enhanced build of OpenJDK source code
o Fully TCK tested
o JDK 7, 8, 11, 13, 15 and 17
o JDK 6 available for commercial customers
• Wide platform support:
o Intel 64-bit Windows, Mac, Linux
o Intel 32-bit Windows and Linux
• Free community edition, optional commercial support (Azul Platform Core)
Thank you!
@speakjava

More Related Content

What's hot

ChaCha20-Poly1305 Cipher Summary - AdaLabs SPARKAda OpenSSH Ciphers
ChaCha20-Poly1305 Cipher Summary - AdaLabs SPARKAda OpenSSH CiphersChaCha20-Poly1305 Cipher Summary - AdaLabs SPARKAda OpenSSH Ciphers
ChaCha20-Poly1305 Cipher Summary - AdaLabs SPARKAda OpenSSH CiphersAdaLabs
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handlingteach4uin
 
Java if else condition - powerpoint persentation
Java if else condition - powerpoint persentationJava if else condition - powerpoint persentation
Java if else condition - powerpoint persentationManeesha Caldera
 
Fantastic DSL in Python
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Pythonkwatch
 
Async code on kotlin: rx java or/and coroutines - Kotlin Night Turin
Async code on kotlin: rx java or/and coroutines - Kotlin Night TurinAsync code on kotlin: rx java or/and coroutines - Kotlin Night Turin
Async code on kotlin: rx java or/and coroutines - Kotlin Night TurinFabio Collini
 
20190625 OpenACC 講習会 第2部
20190625 OpenACC 講習会 第2部20190625 OpenACC 講習会 第2部
20190625 OpenACC 講習会 第2部NVIDIA Japan
 
[CB16] バイナリロックスターになる:Binary Ninjaによるプログラム解析入門 by Sophia D’Antoine
[CB16] バイナリロックスターになる:Binary Ninjaによるプログラム解析入門 by Sophia D’Antoine[CB16] バイナリロックスターになる:Binary Ninjaによるプログラム解析入門 by Sophia D’Antoine
[CB16] バイナリロックスターになる:Binary Ninjaによるプログラム解析入門 by Sophia D’AntoineCODE BLUE
 
Migration Guide from Java 8 to Java 11 #jjug
Migration Guide from Java 8 to Java 11 #jjugMigration Guide from Java 8 to Java 11 #jjug
Migration Guide from Java 8 to Java 11 #jjugYuji Kubota
 
Advanced Sql Injection ENG
Advanced Sql Injection ENGAdvanced Sql Injection ENG
Advanced Sql Injection ENGDmitry Evteev
 
Black Hat EU 2010 - Attacking Java Serialized Communication
Black Hat EU 2010 - Attacking Java Serialized CommunicationBlack Hat EU 2010 - Attacking Java Serialized Communication
Black Hat EU 2010 - Attacking Java Serialized Communicationmsaindane
 
Constructors in java
Constructors in javaConstructors in java
Constructors in javachauhankapil
 

What's hot (20)

集約署名
集約署名集約署名
集約署名
 
ChaCha20-Poly1305 Cipher Summary - AdaLabs SPARKAda OpenSSH Ciphers
ChaCha20-Poly1305 Cipher Summary - AdaLabs SPARKAda OpenSSH CiphersChaCha20-Poly1305 Cipher Summary - AdaLabs SPARKAda OpenSSH Ciphers
ChaCha20-Poly1305 Cipher Summary - AdaLabs SPARKAda OpenSSH Ciphers
 
Java On CRaC
Java On CRaCJava On CRaC
Java On CRaC
 
L14 exception handling
L14 exception handlingL14 exception handling
L14 exception handling
 
Java if else condition - powerpoint persentation
Java if else condition - powerpoint persentationJava if else condition - powerpoint persentation
Java if else condition - powerpoint persentation
 
Fantastic DSL in Python
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Python
 
Async code on kotlin: rx java or/and coroutines - Kotlin Night Turin
Async code on kotlin: rx java or/and coroutines - Kotlin Night TurinAsync code on kotlin: rx java or/and coroutines - Kotlin Night Turin
Async code on kotlin: rx java or/and coroutines - Kotlin Night Turin
 
20190625 OpenACC 講習会 第2部
20190625 OpenACC 講習会 第2部20190625 OpenACC 講習会 第2部
20190625 OpenACC 講習会 第2部
 
[CB16] バイナリロックスターになる:Binary Ninjaによるプログラム解析入門 by Sophia D’Antoine
[CB16] バイナリロックスターになる:Binary Ninjaによるプログラム解析入門 by Sophia D’Antoine[CB16] バイナリロックスターになる:Binary Ninjaによるプログラム解析入門 by Sophia D’Antoine
[CB16] バイナリロックスターになる:Binary Ninjaによるプログラム解析入門 by Sophia D’Antoine
 
Migration Guide from Java 8 to Java 11 #jjug
Migration Guide from Java 8 to Java 11 #jjugMigration Guide from Java 8 to Java 11 #jjug
Migration Guide from Java 8 to Java 11 #jjug
 
Data Encryption Standard
Data Encryption StandardData Encryption Standard
Data Encryption Standard
 
Advanced Sql Injection ENG
Advanced Sql Injection ENGAdvanced Sql Injection ENG
Advanced Sql Injection ENG
 
Exception handling in Java
Exception handling in JavaException handling in Java
Exception handling in Java
 
Black Hat EU 2010 - Attacking Java Serialized Communication
Black Hat EU 2010 - Attacking Java Serialized CommunicationBlack Hat EU 2010 - Attacking Java Serialized Communication
Black Hat EU 2010 - Attacking Java Serialized Communication
 
VerilatorとSystemC
VerilatorとSystemCVerilatorとSystemC
VerilatorとSystemC
 
Sha3
Sha3Sha3
Sha3
 
Methods in Java
Methods in JavaMethods in Java
Methods in Java
 
Java operators
Java operatorsJava operators
Java operators
 
Phpcon2015
Phpcon2015Phpcon2015
Phpcon2015
 
Constructors in java
Constructors in javaConstructors in java
Constructors in java
 

Similar to The Art of Java Type Patterns

Programing with java for begniers .pptx
Programing with java for begniers  .pptxPrograming with java for begniers  .pptx
Programing with java for begniers .pptxadityaraj7711
 
Modern Java Workshop
Modern Java WorkshopModern Java Workshop
Modern Java WorkshopSimon Ritter
 
JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020Joseph Kuo
 
Csharp In Detail Part2
Csharp In Detail Part2Csharp In Detail Part2
Csharp In Detail Part2Mohamed Krar
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)Tak Lee
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentalsHCMUTE
 
Java 5 New Feature
Java 5 New FeatureJava 5 New Feature
Java 5 New Featurexcoda
 
Chap2 class,objects contd
Chap2 class,objects contdChap2 class,objects contd
Chap2 class,objects contdraksharao
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introductioncaswenson
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)teach4uin
 
String and string buffer
String and string bufferString and string buffer
String and string bufferkamal kotecha
 

Similar to The Art of Java Type Patterns (20)

Programing with java for begniers .pptx
Programing with java for begniers  .pptxPrograming with java for begniers  .pptx
Programing with java for begniers .pptx
 
Modern Java Workshop
Modern Java WorkshopModern Java Workshop
Modern Java Workshop
 
Pattern Matching in Java 14
Pattern Matching in Java 14Pattern Matching in Java 14
Pattern Matching in Java 14
 
JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020JCConf 2020 - New Java Features Released in 2020
JCConf 2020 - New Java Features Released in 2020
 
core java
 core java core java
core java
 
Csharp In Detail Part2
Csharp In Detail Part2Csharp In Detail Part2
Csharp In Detail Part2
 
131 Lab slides (all in one)
131 Lab slides (all in one)131 Lab slides (all in one)
131 Lab slides (all in one)
 
Java Cheat Sheet
Java Cheat SheetJava Cheat Sheet
Java Cheat Sheet
 
Computer programming 2 Lesson 10
Computer programming 2  Lesson 10Computer programming 2  Lesson 10
Computer programming 2 Lesson 10
 
Java 5 Features
Java 5 FeaturesJava 5 Features
Java 5 Features
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
Java 5 New Feature
Java 5 New FeatureJava 5 New Feature
Java 5 New Feature
 
Java String Handling
Java String HandlingJava String Handling
Java String Handling
 
java-programming.pdf
java-programming.pdfjava-programming.pdf
java-programming.pdf
 
Chap2 class,objects contd
Chap2 class,objects contdChap2 class,objects contd
Chap2 class,objects contd
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 

More from Simon Ritter

Cloud Native Compiler
Cloud Native CompilerCloud Native Compiler
Cloud Native CompilerSimon Ritter
 
Java performance monitoring
Java performance monitoringJava performance monitoring
Java performance monitoringSimon Ritter
 
Modern Java Workshop
Modern Java WorkshopModern Java Workshop
Modern Java WorkshopSimon Ritter
 
Getting the Most From Modern Java
Getting the Most From Modern JavaGetting the Most From Modern Java
Getting the Most From Modern JavaSimon Ritter
 
Building a Better JVM
Building a Better JVMBuilding a Better JVM
Building a Better JVMSimon Ritter
 
JDK 14 Lots of New Features
JDK 14 Lots of New FeaturesJDK 14 Lots of New Features
JDK 14 Lots of New FeaturesSimon Ritter
 
How to Choose a JDK
How to Choose a JDKHow to Choose a JDK
How to Choose a JDKSimon Ritter
 
The Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologyThe Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologySimon Ritter
 
Developing Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java TechnologyDeveloping Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java TechnologySimon Ritter
 
Is Java Still Free?
Is Java Still Free?Is Java Still Free?
Is Java Still Free?Simon Ritter
 
Moving Towards JDK 12
Moving Towards JDK 12Moving Towards JDK 12
Moving Towards JDK 12Simon Ritter
 
JDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and BeyondJDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and BeyondSimon Ritter
 
Java Is Still Free
Java Is Still FreeJava Is Still Free
Java Is Still FreeSimon Ritter
 
JDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and BeyondJDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and BeyondSimon Ritter
 
JDK 9 and JDK 10 Deep Dive
JDK 9 and JDK 10 Deep DiveJDK 9 and JDK 10 Deep Dive
JDK 9 and JDK 10 Deep DiveSimon Ritter
 
Java Support: What's changing
Java Support:  What's changingJava Support:  What's changing
Java Support: What's changingSimon Ritter
 
JDK 9: The Start of a New Future for Java
JDK 9: The Start of a New Future for JavaJDK 9: The Start of a New Future for Java
JDK 9: The Start of a New Future for JavaSimon Ritter
 

More from Simon Ritter (20)

Cloud Native Compiler
Cloud Native CompilerCloud Native Compiler
Cloud Native Compiler
 
Java performance monitoring
Java performance monitoringJava performance monitoring
Java performance monitoring
 
Modern Java Workshop
Modern Java WorkshopModern Java Workshop
Modern Java Workshop
 
Getting the Most From Modern Java
Getting the Most From Modern JavaGetting the Most From Modern Java
Getting the Most From Modern Java
 
Building a Better JVM
Building a Better JVMBuilding a Better JVM
Building a Better JVM
 
JDK 14 Lots of New Features
JDK 14 Lots of New FeaturesJDK 14 Lots of New Features
JDK 14 Lots of New Features
 
Java after 8
Java after 8Java after 8
Java after 8
 
How to Choose a JDK
How to Choose a JDKHow to Choose a JDK
How to Choose a JDK
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
The Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans TechnologyThe Latest in Enterprise JavaBeans Technology
The Latest in Enterprise JavaBeans Technology
 
Developing Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java TechnologyDeveloping Enterprise Applications Using Java Technology
Developing Enterprise Applications Using Java Technology
 
Is Java Still Free?
Is Java Still Free?Is Java Still Free?
Is Java Still Free?
 
Moving Towards JDK 12
Moving Towards JDK 12Moving Towards JDK 12
Moving Towards JDK 12
 
JDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and BeyondJDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and Beyond
 
Java Is Still Free
Java Is Still FreeJava Is Still Free
Java Is Still Free
 
JDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and BeyondJDK 9, 10, 11 and Beyond
JDK 9, 10, 11 and Beyond
 
JDK 9 and JDK 10 Deep Dive
JDK 9 and JDK 10 Deep DiveJDK 9 and JDK 10 Deep Dive
JDK 9 and JDK 10 Deep Dive
 
JDK 9 Deep Dive
JDK 9 Deep DiveJDK 9 Deep Dive
JDK 9 Deep Dive
 
Java Support: What's changing
Java Support:  What's changingJava Support:  What's changing
Java Support: What's changing
 
JDK 9: The Start of a New Future for Java
JDK 9: The Start of a New Future for JavaJDK 9: The Start of a New Future for Java
JDK 9: The Start of a New Future for Java
 

Recently uploaded

DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackVICTOR MAESTRE RAMIREZ
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningVitsRangannavar
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsMehedi Hasan Shohan
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 

Recently uploaded (20)

DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Cloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStackCloud Management Software Platforms: OpenStack
Cloud Management Software Platforms: OpenStack
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
cybersecurity notes for mca students for learning
cybersecurity notes for mca students for learningcybersecurity notes for mca students for learning
cybersecurity notes for mca students for learning
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
XpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software SolutionsXpertSolvers: Your Partner in Building Innovative Software Solutions
XpertSolvers: Your Partner in Building Innovative Software Solutions
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 

The Art of Java Type Patterns

  • 1. The Art of Java Type Patterns Simon Ritter, Deputy CTO | Azul
  • 2. 2 Modern Java • The six-month release cadence for Java has been really good • Lots of new features added much faster than we've ever seen before • Significant language changes are initially developed under project Amber o "... explore and incubate smaller, productivity-oriented Java language features..." o Most features go through at least two rounds of preview • Many of the new features work both separately and combined with others
  • 3. 3 Pattern Matching In Java • java.util.regex • This is not what we're here to talk about Pattern p = Pattern.compile("a*b"); Matcher m = p.matcher("aaaaab"); boolean b = m.matches(); boolean b = Pattern.matches("a*b", "aaaaab");
  • 4. 4 Pattern Matching Fundamentals • A well used technique, been in use since the 1960s (used in Haskell, AWK, etc.) match predicate pattern variables Determines whether the pattern matches a target A pattern Conditionally extracted if the pattern matches the target
  • 5. 5 Pattern Types • Constant o Match on a constant (already in use in a switch statement) • Type o Match on a type • Deconstruction o Match and extract • var o Uses type inference to map to a type pattern (effecively matches anything) • Any (_) o Matches anything but binds to nothing (an unused pattern variable). See JEP 302
  • 6. 6 Before we get into to Pattern Matching
  • 7. 7 Switch Expressions • Switch construct was a statement o No concept of generating a result that could be assigned • Rather clunky syntax o Every case statement needs to be separated o Must remember break (default is to fall through) o Scope of local variables is not intuitive
  • 8. 8 Switch Statement int numberOfLetters; switch (day) { case MONDAY: case FRIDAY: case SUNDAY: numberOfLetters = 6; break; case TUESDAY: numberOfLetters = 7; break; case THURSDAY: case SATURDAY: numberOfLetters = 8; break; case WEDNESDAY: numberOfLetters = 9; break; default: throw new IllegalStateException("Huh?: " + day); };
  • 9. 9 Switch Expression (JDK 12) • Switch expressions must be complete (exhaustive) o We'll come back to this later int numberOfLetters = switch (day) { case MONDAY, FRIDAY, SUNDAY -> 6; case TUESDAY -> 7; case THURSDAY, SATURDAY -> 8; case WEDNESDAY -> 9; default -> throw new IllegalStateException("Huh?: " + day); };
  • 11. 11 Simple Java Data Class class Point { private final double x; private final double y; public Point(double x, double y) { this.x = x; this.y = y; } public double x() { return x; } public double y() { return y; } }
  • 12. 12 Records (JDK 14) record Point(double x, double y) { } record Anything<T>(T t) { } // Generic Record public record Circle(double radius) { private static final double PI = 3.142; // Static instance fields are allowed public double area() { return PI * radius * radius; } }
  • 13. 13 Records Additional Details • The base class of all records is java.lang.Record o Records cannot sub-class (but may implement interfaces) • Object methods equals(), hashCode() and toString() can be overridden • Records are implicitly final (although you may add the modifier) • Records do not follow the Java bean pattern o x() not getX() in Point example o record Point(getX, getY) // If you must
  • 14. 14 Java Inheritance • A class (or interface) in Java can be sub-classed by any class o Unless it is marked as final Shape Triangle Square Pentagon
  • 15. 15 Sealed Classes (JEP 360) public sealed class Shape permits Triangle, Square, Pentagon { ... } Shape Triangle Square Pentagon Circle X
  • 16. 16 Sealed Classes (JEP 360) • All sub-classes must have inheritance capabilities explicitly specified // Restrict sub-classes to defined set public sealed class Triangle permits Equilateral, Isosoles extends Shape { ... } // Prevent any further sub-classing public final class Square extends Shape { ... } // Allow any classes to sub-class this one (open) public non-sealed class Pentagon extends Shape { ... }
  • 18. 18 Using The instanceof Operator if (obj instanceof String) { String s = (String)obj; System.out.println(s.length()); } We must always perform an explicit cast with an assignment
  • 19. 19 Pattern Matching For instanceof (JDK 14) if (obj instanceof String s) System.out.println(s.length()); else // Use of s not allowed here if (obj instanceof String s && s.length() > 0) System.out.println(s.length()); // Compiler error if (obj instanceof String s || s.length() > 0) System.out.println(s.length());
  • 20. 20 Pattern Matching For instanceof public void doSomething(Object o) { if (!(o instanceof String s)) return; System.out.println(s.length()); // Scope of s valid // Several hundred lines of code System.out.println(s.length()); // Still in scope }
  • 21. 22 Flow Scoping For Binding Variables • Scope of local variable runs from its declaration until the end of the block in which it is declared o Locals are subject to definite assigment • Binding variables are also subject to definite assignment o The scope of a binding variable is the set of places in the program where it would be definitely assigned o This is flow scoping • However, scope is not the same as local variables if (o instanceof Integer num) { ... } else if (o instanceof Float num) { ... } else if (o instanceof Long num) { ... } Need flow scoping to be able to reuse num as variable name
  • 22. 23 Pattern Matching For instanceof Puzzler • Will this work? Object s = new Object(); if (s instanceof String s) System.out.println("String of length " + s.length()); else System.out.println("No string");
  • 23. 25 Pattern Matching For switch • Switch is limited on what types you can use (Integral values, Strings, enumerations) • Expanded to allow type patterns to be matched void typeTester(Object o) { switch (o) { case null -> System.out.println("Null type"); case String s -> System.out.println("String: " + s); case Color c -> System.out.println("Color with RGB: " + c.getRGB()); case int[] ia -> System.out.println("Array of ints, length" + ia.length); default -> System.out.println(o.toString()); } }
  • 24. 26 Pattern Matching For switch • Null is special (and complicated) • case null can be used in all switch statements and expressions o If not included, it will be added by compiler at start (throwing NullPointerException) void typeTester(Object o) { switch (o) { case String s -> System.out.println("String: " + s); case Color c -> System.out.println("Color with RGB: " + c.getRGB()); case int[] ia -> System.out.println("Array of ints, length" + ia.length); default -> System.out.println("Bad input!"); } }
  • 25. 27 Pattern Matching For switch • Null is special (and complicated) • case null can be used in all switch statements and expressions o If not included, it will be added by compiler at start (throwing NullPointerException) void typeTester(Object o) { switch (o) { case null -> throw new NullPointerException(); // Added by compiler case String s -> System.out.println("String: " + s); case Color c -> System.out.println("Color with RGB: " + c.getRGB()); case int[] ia -> System.out.println("Array of ints, length" + ia.length); default -> System.out.println("Bad input!"); } }
  • 26. 28 Pattern Matching For switch • Null is special (and complicated) • case null can be used in all switch statements and expressions o If not included, it will be added by compiler at start (throwing NullPointerException) void typeTester(Object o) { switch (o) { case String s -> System.out.println("String: " + s); case Color c -> System.out.println("Color with RGB: " + c.getRGB()); case int[] ia -> System.out.println("Array of ints, length" + ia.length); null, default -> System.out.println("Bad input!"); // No NullPointerException } }
  • 27. 29 Pattern Matching for switch (Completeness) • Pattern switch statements (and all switch expressions) must be exhaustive o All possible values must be handled void typeTester(Object o) { switch (o) { case String s -> System.out.println("String: " + s); case Integer i -> System.out.println("Integer with value " + i.getInteger()); } } void typeTester(Object o) { switch (o) { case String s -> System.out.println("String: " + s); case Integer i -> System.out.println("Integer with value " + i.getInteger()); default -> System.out.println("Some other type"); } }
  • 28. 30 Pattern Matching for switch (Completeness) void typeTester(Shape shape) { // Using previous sealed class example switch (shape) { case Triangle t -> System.out.println("It's a triangle"); case Square s -> System.out.println("It's a square"); case Pentagon p -> System.out.println("It's a pentagon"); case Shape s -> System.out.println("It's a shape"); } }
  • 29. 31 Guarded Patterns void shapeTester(Shape shape) { // Using previous sealed class example switch (shape) { case Triangle t && t.area() > 25 -> System.out.println("It's a big triangle"); case Triangle t -> System.out.println("It's a small triangle"); case Square s -> System.out.println("It's a square"); case Pentagon p -> System.out.println("It's a pentagon"); case Shape s -> System.out.println("It's a shape"); } } GuardedPattern: PrimaryPattern && ConditionalAndExpression
  • 30. 32 Pattern Dominance • Less specific cases must not hide more specific cases void typeTester(Shape shape) { switch (shape) { case Shape s -> System.out.println("It's a shape"); case Triangle t -> System.out.println("It's a triangle"); case Square s -> System.out.println("It's a square"); case Pentagon p -> System.out.println("It's a pentagon"); } } Shape will always match first Triangle, Square, Pentagon cases are unreachable
  • 31. 33 Pattern Dominance void shapeTester(Shape shape) { switch (shape) { case Triangle t -> System.out.println("It's a small triangle"); case Triangle t && t.area() > 25 -> System.out.println("It's a big triangle"); case Square s -> System.out.println("It's a square"); case Pentagon p -> System.out.println("It's a pentagon"); case Shape s -> System.out.println("It's a shape"); } } Again, Triangle will match before Triangle with a guard
  • 32. 34 Pattern Matching in Future Java
  • 33. 35 Pattern Matching instanceof And Records • What we can do now • This is good but we can do better record Point(double x, double y) { } public void pythagoras(Object o) { if (o instanceof Point p) { double x = p.x(); double y = p.y(); System.out.println("Hypotonuse = " + Math.sqrt((x*x) + (y*y)); } }
  • 34. 36 Pattern Matching For Records (JEP 405) • Use of a record pattern (which is a deconstruction pattern) • Since a Record is just a special form of class, this will work with normal classes as well public void pythagoras(Object o) { if (o instanceof Point(double x, double y)) System.out.println("Hypotonuse = " + Math.sqrt((x*x) + (y*y)); }
  • 35. 37 Patterns Are Composable record Point(double x, double y) {}; enum Colour { RED, GREEN, BLUE }; record ColourPoint(Point p, Colour c) {}; record ColourRectangle(ColourPoint topLeft, ColourPoint bottomRight) implements Shape { ... }; public void printColour(Shape s) { if (s instanceof ColourRectangle(ColourPoint topleft, ColourPoint bottomRight)) System.out.println(topLeft.c()); } But this is a record as well
  • 36. 38 Patterns Are Composable public void printColour(Shape s) { if (s instanceof ColourRectangle(ColourPoint(Point p, Colour c), ColourPoint br)) System.out.println(c); } public void printTopLeftX(Shape s) { if (s instanceof ColourRectangle(ColourPoint(Point(double x, double y), Colour c), ColourPoint br) System.out.println(x); } Yet another record
  • 37. 39 Patterns And Local Variable Type Inference • Use var, introduced in JDK 10 • An example of the any pattern matching type public void printTopLeftX(Shape s) { if (s instanceof ColourRectangle(ColourPoint(Point(var x, var y), var c), var br) System.out.println(x); }
  • 38. 40 Using The Any Pattern Match • Not yet part of any proposed JEP • Related to JEP 302, Lambda Leftovers • This is not a proposed change, at this time (i.e. I'm speculating this as a feature) public void printTopLeftX(Shape s) { if (s instanceof ColourRectangle(ColourPoint(Point(var x, _), _), _) System.out.println(x); } We have no interest in these variables so let's ignore them
  • 39. 41 Pattern Matching For Arrays • Why not use a decompose pattern for arrays? • This was part of JEP 405 but has been dropped for now static void printFirstTwoStrings(Object o) { if (o instanceof String[] sa && sa.length >= 2) { String s1 = sa[0]; String s2 = sa[1]; System.out.println(s1 + s2); } } static void printFirstTwoStrings(Object o) { if (o instanceof String[] { String s1, String s2, ... }) System.out.println(s1 + s2); }
  • 41. 43 Conclusions • Pattern matching is a powerful set of language constructs • Simplifies certain tasks o Less boilerplate code o More declarative • Also has the potential for better optimisation of code and enhanced performance • Some features already included • More to come • Yet more may be added later
  • 42. 44 Azul Zulu Builds of OpenJDK • Enhanced build of OpenJDK source code o Fully TCK tested o JDK 7, 8, 11, 13, 15 and 17 o JDK 6 available for commercial customers • Wide platform support: o Intel 64-bit Windows, Mac, Linux o Intel 32-bit Windows and Linux • Free community edition, optional commercial support (Azul Platform Core)