SlideShare una empresa de Scribd logo
1 de 60
1
Java 14 Pattern Matching
Oleksandr Navka
Lead Software Engineer, Consultant, GlobalLogic
2
1. Updated instanceof
2. Updated switch-case
3. Java Records
Agenda
3
Тагир Валеев
JetBrains
Pattern matching
и его воображаемые друзья
4
5
When someone match something :)
What is pattern matching?
6
Any pattern
• _
Constant pattern
• 1
• true
• null
Type pattern
• String s
Var pattern
• var i = 1
Deconstruction pattern
• Optional(String s)
• Node(Node left, Node right)
What are the patterns?
7
Instanceof
8
public class Animal {
}
class Dog extends Animal {
public void bark() {
}
}
class Cat extends Animal {
public void meow() {
}
}
9
public static void say(Animal animal) {
if (animal instanceof Dog) {
Dog dog = (Dog) animal;
dog.bark();
} else if (animal instanceof Cat) {
Cat cat = (Cat) animal;
cat.meow();
}
}
10
public static void say(Animal animal) {
if (animal instanceof Dog) {
Dog dog = (Dog) animal;
dog.bark();
} else if (animal instanceof Cat) {
Cat cat = (Cat) animal;
cat.meow();
}
}
11
public static void say(Animal animal) {
if (animal instanceof Dog dog) {
dog.bark();
} else if (animal instanceof Cat cat) {
cat.meow();
}
}
12
public static void say(Animal animal) {
if (animal instanceof Dog dog) {
dog.bark();
} else if (animal instanceof Cat cat) {
cat.meow();
}
}
13
Before:
expression istanceof Type
Now:
expression instanceof Pattern
14
Scoping
15
if (obj instanceof String s) {
// can use s here
} else {
// can't use s here
}
16
if (obj instanceof String s) {
// can use s here
} else {
// can't use s here
}
binding variable
17
if (!(obj instanceof String s)) {
} else {
s.contains("a")
}
18
if(obj instanceof String s && s.length() > 5) {
System.out.println(s.toUpperCase());
}
19
if(obj instanceof String s || s.length() > 5) {
System.out.println(s.toUpperCase());
}
20
if(!(obj instanceof String s) || s.length() > 5) {
System.out.println("Do something");
} else {
System.out.println(s.toUpperCase());
}
21
if(!(obj instanceof String s) || s.length() > 5) {
System.out.println("Do something");
} else {
System.out.println(s.toUpperCase());
}
22
if(!(obj instanceof String s) || s.length() > 5) {
throw new IllegalArgumentException();
}
System.out.println(s.toUpperCase());
23
if(!(obj instanceof String s) || s.length() > 5) {
throw new IllegalArgumentException();
}
System.out.println(s.toUpperCase());
24
if (!(obj instanceof String s)) {
s.contains(..)
} else {
s.contains(..)
}
25
private String s = "abc";
if (!(obj instanceof String s)) {
s.contains(..)
} else {
s.contains(..)
}
26
1. if (obj instanceof String s) {s.trim()}
2. if (!(obj instanceof String s)) {} else {s.contains("a")}
3. if(obj instanceof String s && s.length() > 5) {}
4. if(obj instanceof String s || s.length() > 5) {}
Binding variable scope
27
1. if (obj instanceof String s) {s.trim()}
2. if (!(obj instanceof String s)) {} else {s.contains("a")}
3. if(obj instanceof String s && s.length() > 5) {}
4. if(obj instanceof String s || s.length() > 5) {}
Binding variable scope
28
1. if (obj instanceof String s) {s.trim()}
2. if (!(obj instanceof String s)) {} else {s.contains("a")}
3. if(obj instanceof String s && s.length() > 5) {}
4. if(obj instanceof String s || s.length() > 5) {}
5. if(!(obj instanceof String s) || s.length() > 5) {}
6. if(!(obj instanceof String s) || s.length() > 5) {} else {s.toUpperCase());}
7. if(!(obj instanceof String s) || s.length() > 5) {
throw new IllegalArgumentException();
}
System.out.println(s.toUpperCase());
Binding variable scope
29
1. instanceof has got type pattern matching
2. instanceof the first java feature that got pattern matching
3. Be aware about scoping
Instanceof Summary
30
Switch - case
31
public enum ProgrammingParadigm {OBJECT_ORIENTED, PROCEDURAL, FUNCTIONAL}
public static List<String> getProgrammingLanguage(ProgrammingParadigm programmingParadigm) {
List<String> languages = new ArrayList<>();
switch (programmingParadigm) {
case OBJECT_ORIENTED:
languages.add("Java");
languages.add("C++");
break;
case FUNCTIONAL:
languages.add("Haskel");
break;
case PROCEDURAL:
languages.add("Pascal");
break;
}
return languages;
}
Old version of switch
32
public enum ProgrammingParadigm {OBJECT_ORIENTED, PROCEDURAL, FUNCTIONAL}
public static List<String> getProgrammingLanguage(ProgrammingParadigm programmingParadigm) {
List<String> languages = new ArrayList<>();
switch (programmingParadigm) {
case OBJECT_ORIENTED:
languages.add("Java");
languages.add("C++");
break;
case FUNCTIONAL:
languages.add("Haskel");
break;
case PROCEDURAL:
languages.add("Pascal");
break;
}
return languages;
}
Old version of switch
33
public enum ProgrammingParadigm {OBJECT_ORIENTED, PROCEDURAL, FUNCTIONAL}
public static List<String> getProgrammingLanguage(ProgrammingParadigm programmingParadigm) {
List<String> languages = new ArrayList<>();
switch (programmingParadigm) {
case OBJECT_ORIENTED:
languages.add("Java");
languages.add("C++");
break;
case FUNCTIONAL:
languages.add("Haskel");
break;
case PROCEDURAL:
languages.add("Pascal");
break;
}
return languages;
}
Old version of switch
Input type limitations:
byte, short, char, and int
Enum Types, String
34
public enum ProgrammingParadigm {OBJECT_ORIENTED, PROCEDURAL, FUNCTIONAL}
public static List<String> getProgrammingLanguage(ProgrammingParadigm programmingParadigm) {
List<String> languages = new ArrayList<>();
switch (programmingParadigm) {
case OBJECT_ORIENTED:
languages.add("Java");
languages.add("C++");
break;
case FUNCTIONAL:
languages.add("Haskel");
break;
case PROCEDURAL:
languages.add("Pascal");
break;
}
return languages;
}
Old version of switch
Can’t
get
result
35
public static List<String> getProgrammingLanguage(ProgrammingParadigm programmingParadigm) {
switch (programmingParadigm) {
case OBJECT_ORIENTED:
return List.of("Java", "C++");
case FUNCTIONAL:
return List.of("Haskel");
case PROCEDURAL:
return List.of("Pascal");
default:
return List.of();
}
}
36
public static List<String> getProgrammingLanguage(ProgrammingParadigm programmingParadigm) {
switch (programmingParadigm) {
case OBJECT_ORIENTED:
return List.of("Java", "C++");
case FUNCTIONAL:
return List.of("Haskel");
case PROCEDURAL:
return List.of("Pascal");
default:
return List.of();
}
}
37
Disadvantages:
1. Always remember about break
2. Input data type limitation
3. Can’t get result
4. Don’t understand all possible options for enum
Old version of switch
38
Pattern matching with switch case
39
public void printObject(Object obj) {
switch (obj) {
case String s:
System.out.println("This is String" + s.trim());
break;
case Integer i:
System.out.println("This is Integer" + i);
break;
case Number n:
System.out.println("This is number"+ n);
break;
default:
System.out.println("I don't know");
}
}
40
public void printObject(Object obj) {
switch (obj) {
case String s:
System.out.println("This is String" + s.trim());
break;
case Number n:
System.out.println("This is number"+ n);
break;
case Integer i:
System.out.println("This is Integer" + i);
break;
default:
System.out.println("I don't know");
}
}
41
public void printObject(Object obj) {
switch (obj) {
case String s:
System.out.println("This is String" + s.trim());
break;
default:
System.out.println("I don't know");
case Number n:
System.out.println("This is number"+ n);
break;
case Integer i:
System.out.println("This is Integer" + i);
break;
}
}
42
Updated switch case
43
public static List<String> getProgrammingLanguage(ProgrammingParadigm programmingParadigm) {
return switch (programmingParadigm) {
case OBJECT_ORIENTED -> List.of("Java", "C++");
case FUNCTIONAL -> List.of("Haskel");
case PROCEDURAL -> List.of("Pascal");
};
}
Updated switch case
44
public static List<String> getProgrammingLanguage(ProgrammingParadigm programmingParadigm) {
return switch (programmingParadigm) {
case OBJECT_ORIENTED -> {
System.out.println("You are master of polymorphism");
yield List.of("Java", "C++");
}
case FUNCTIONAL -> {
System.out.println("You are master of recursion");
yield List.of("Haskel");
}
case PROCEDURAL -> {
System.out.println("Try to use another paradigms");
yield List.of("Pascal");
}
};
}
45
1. Don’t need to use break
2. Can return result
3. Don’t need to use default branch if all options described
4. Input data types have limitations
Summary of updated switch
46
Java Records
47
private record Car(int speed, String manufacturer) {
}
Java Records
48
private record Car(int speed, String manufacturer) {
}
public static void main(String[] args) {
Car car = new Car(100, "Mercedes");
System.out.println(car.speed());
System.out.println(car.manufacturer());
}
Java Records
49
private record Car(int speed, String manufacturer) {
}
public static void main(String[] args) {
Car car = new Car(100, "Mercedes");
System.out.println(car);
}
-------
Output:
Car[speed=100, manufacturer=Mercedes]
Java Records
50
private static final class Car extends java.lang.Record {
private final int speed;
private final java.lang.String manufacturer;
public Car(int speed, java.lang.String manufacturer) { /* compiled code */ }
public java.lang.String toString() { /* compiled code */ }
public final int hashCode() { /* compiled code */ }
public final boolean equals(java.lang.Object o) { /* compiled code */ }
public int speed() { /* compiled code */ }
public java.lang.String manufacturer() { /* compiled code */ }
}
51
private static final class Car extends java.lang.Record {
private final int speed;
private final java.lang.String manufacturer;
public Car(int speed, java.lang.String manufacturer) { /* compiled code */ }
public java.lang.String toString() { /* compiled code */ }
public final int hashCode() { /* compiled code */ }
public final boolean equals(java.lang.Object o) { /* compiled code */ }
public int speed() { /* compiled code */ }
public java.lang.String manufacturer() { /* compiled code */ }
}
52
public abstract class Record {
protected Record() {}
@Override
public abstract boolean equals(Object obj);
@Override
public abstract int hashCode();
@Override
public abstract String toString();
}
53
private static final class Car extends java.lang.Record {
private final int speed;
private final java.lang.String manufacturer;
public Car(int speed, java.lang.String manufacturer) { /* compiled code */ }
public java.lang.String toString() { /* compiled code */ }
public final int hashCode() { /* compiled code */ }
public final boolean equals(java.lang.Object o) { /* compiled code */ }
public int speed() { /* compiled code */ }
public java.lang.String manufacturer() { /* compiled code */ }
}
54
private static final class Car extends java.lang.Record {
private final int speed;
private final java.lang.String manufacturer;
public Car(int speed, java.lang.String manufacturer) { /* compiled code */ }
public java.lang.String toString() { /* compiled code */ }
public final int hashCode() { /* compiled code */ }
public final boolean equals(java.lang.Object o) { /* compiled code */ }
public int speed() { /* compiled code */ }
public java.lang.String manufacturer() { /* compiled code */ }
}
55
private record Car(int speed, String manufacturer) {
public Car {
if(speed < 0) {
throw new IllegalArgumentException();
}
}
}
public static void main(String[] args) {
var car = new Car(-2, "Mercedes");
System.out.println(car);
}
Canonical constructor
56
private record Car(int speed, String manufacturer) {
public Car {
if(speed < 0) {
throw new IllegalArgumentException();
}
}
public Car(String manufacturer) {
this(0, manufacturer);
}
}
Canonical constructor
57
private static final class Car extends java.lang.Record {
private final int speed;
private final java.lang.String manufacturer;
public Car(int speed, java.lang.String manufacturer) { /* compiled code */ }
public java.lang.String toString() { /* compiled code */ }
public final int hashCode() { /* compiled code */ }
public final boolean equals(java.lang.Object o) { /* compiled code */ }
public int speed() { /* compiled code */ }
public java.lang.String manufacturer() { /* compiled code */ }
}
58
private static final class Car extends java.lang.Record {
private final int speed;
private final java.lang.String manufacturer;
public Car(int speed, java.lang.String manufacturer) { /* compiled code */ }
public java.lang.String toString() { /* compiled code */ }
public final int hashCode() { /* compiled code */ }
public final boolean equals(java.lang.Object o) { /* compiled code */ }
public int speed() { /* compiled code */ }
public java.lang.String manufacturer() { /* compiled code */ }
}
59
1. Java 14 present first version of pattern matching
2. Type pattern matching present in instanceof operator
3. New Switch statement was released with some blanks for
pattern matching
4. Keep eye on Java Records and deconstruction patterns
Summary
60
Thank You

Más contenido relacionado

La actualidad más candente

Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesGanesh Samarthyam
 
Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - StockholmJan Kronquist
 
02 Java Language And OOP Part II LAB
02 Java Language And OOP Part II LAB02 Java Language And OOP Part II LAB
02 Java Language And OOP Part II LABHari Christian
 
Beauty and the beast - Haskell on JVM
Beauty and the beast  - Haskell on JVMBeauty and the beast  - Haskell on JVM
Beauty and the beast - Haskell on JVMJarek Ratajski
 
02 Java Language And OOP PART II
02 Java Language And OOP PART II02 Java Language And OOP PART II
02 Java Language And OOP PART IIHari Christian
 
Kotlin 101 for Java Developers
Kotlin 101 for Java DevelopersKotlin 101 for Java Developers
Kotlin 101 for Java DevelopersChristoph Pickl
 
Kotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime PerformanceKotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime Performanceintelliyole
 
Java Keeps Throttling Up!
Java Keeps Throttling Up!Java Keeps Throttling Up!
Java Keeps Throttling Up!José Paumard
 
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions Ganesh Samarthyam
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesGanesh Samarthyam
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...julien.ponge
 
JavaOne 2013 - Clojure for Java Developers
JavaOne 2013 - Clojure for Java DevelopersJavaOne 2013 - Clojure for Java Developers
JavaOne 2013 - Clojure for Java DevelopersJan Kronquist
 

La actualidad más candente (20)

Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
 
Clojure for Java developers - Stockholm
Clojure for Java developers - StockholmClojure for Java developers - Stockholm
Clojure for Java developers - Stockholm
 
Comparing JVM languages
Comparing JVM languagesComparing JVM languages
Comparing JVM languages
 
02 Java Language And OOP Part II LAB
02 Java Language And OOP Part II LAB02 Java Language And OOP Part II LAB
02 Java Language And OOP Part II LAB
 
Beauty and the beast - Haskell on JVM
Beauty and the beast  - Haskell on JVMBeauty and the beast  - Haskell on JVM
Beauty and the beast - Haskell on JVM
 
Java Concurrency by Example
Java Concurrency by ExampleJava Concurrency by Example
Java Concurrency by Example
 
Eta
EtaEta
Eta
 
Sailing with Java 8 Streams
Sailing with Java 8 StreamsSailing with Java 8 Streams
Sailing with Java 8 Streams
 
02 Java Language And OOP PART II
02 Java Language And OOP PART II02 Java Language And OOP PART II
02 Java Language And OOP PART II
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
Kotlin 101 for Java Developers
Kotlin 101 for Java DevelopersKotlin 101 for Java Developers
Kotlin 101 for Java Developers
 
Kotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime PerformanceKotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime Performance
 
Kotlin meets Gadsu
Kotlin meets GadsuKotlin meets Gadsu
Kotlin meets Gadsu
 
Java Keeps Throttling Up!
Java Keeps Throttling Up!Java Keeps Throttling Up!
Java Keeps Throttling Up!
 
Strings and Characters
Strings and CharactersStrings and Characters
Strings and Characters
 
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
OCP Java SE 8 Exam - Sample Questions - Lambda Expressions
 
Design Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on ExamplesDesign Patterns - Compiler Case Study - Hands-on Examples
Design Patterns - Compiler Case Study - Hands-on Examples
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
 
JavaOne 2013 - Clojure for Java Developers
JavaOne 2013 - Clojure for Java DevelopersJavaOne 2013 - Clojure for Java Developers
JavaOne 2013 - Clojure for Java Developers
 

Similar a Pattern Matching in Java 14

The Art of Java Type Patterns
The Art of Java Type PatternsThe Art of Java Type Patterns
The Art of Java Type PatternsSimon Ritter
 
JavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingJavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingHenri Tremblay
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldBTI360
 
Nice to meet Kotlin
Nice to meet KotlinNice to meet Kotlin
Nice to meet KotlinJieyi Wu
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Jesper Kamstrup Linnet
 
Evolution and Examples of Java Features, from Java 1.7 to Java 22
Evolution and Examples of Java Features, from Java 1.7 to Java 22Evolution and Examples of Java Features, from Java 1.7 to Java 22
Evolution and Examples of Java Features, from Java 1.7 to Java 22Yann-Gaël Guéhéneuc
 
The Future of JVM Languages
The Future of JVM Languages The Future of JVM Languages
The Future of JVM Languages VictorSzoltysek
 
Not your father's tests
Not your father's testsNot your father's tests
Not your father's testsSean P. Floyd
 
GTAC 2014: What lurks in test suites?
GTAC 2014: What lurks in test suites?GTAC 2014: What lurks in test suites?
GTAC 2014: What lurks in test suites?Patrick Lam
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghStuart Roebuck
 
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Ayes Chinmay
 

Similar a Pattern Matching in Java 14 (20)

What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 
The Art of Java Type Patterns
The Art of Java Type PatternsThe Art of Java Type Patterns
The Art of Java Type Patterns
 
Java 5 and 6 New Features
Java 5 and 6 New FeaturesJava 5 and 6 New Features
Java 5 and 6 New Features
 
JavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programmingJavaOne 2016 - Learn Lambda and functional programming
JavaOne 2016 - Learn Lambda and functional programming
 
Forgive me for i have allocated
Forgive me for i have allocatedForgive me for i have allocated
Forgive me for i have allocated
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
 
Scala - en bedre Java?
Scala - en bedre Java?Scala - en bedre Java?
Scala - en bedre Java?
 
Nice to meet Kotlin
Nice to meet KotlinNice to meet Kotlin
Nice to meet Kotlin
 
Scala
ScalaScala
Scala
 
Scala in a nutshell by venkat
Scala in a nutshell by venkatScala in a nutshell by venkat
Scala in a nutshell by venkat
 
Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?Scala - en bedre og mere effektiv Java?
Scala - en bedre og mere effektiv Java?
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
 
Evolution and Examples of Java Features, from Java 1.7 to Java 22
Evolution and Examples of Java Features, from Java 1.7 to Java 22Evolution and Examples of Java Features, from Java 1.7 to Java 22
Evolution and Examples of Java Features, from Java 1.7 to Java 22
 
The Future of JVM Languages
The Future of JVM Languages The Future of JVM Languages
The Future of JVM Languages
 
Not your father's tests
Not your father's testsNot your father's tests
Not your father's tests
 
Introduzione al TDD
Introduzione al TDDIntroduzione al TDD
Introduzione al TDD
 
Beyond java8
Beyond java8Beyond java8
Beyond java8
 
GTAC 2014: What lurks in test suites?
GTAC 2014: What lurks in test suites?GTAC 2014: What lurks in test suites?
GTAC 2014: What lurks in test suites?
 
Scala @ TechMeetup Edinburgh
Scala @ TechMeetup EdinburghScala @ TechMeetup Edinburgh
Scala @ TechMeetup Edinburgh
 
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
 

Más de GlobalLogic Ukraine

GlobalLogic Java Community Webinar #17 “SpringJDBC vs JDBC. Is Spring a Hero?”
GlobalLogic Java Community Webinar #17 “SpringJDBC vs JDBC. Is Spring a Hero?”GlobalLogic Java Community Webinar #17 “SpringJDBC vs JDBC. Is Spring a Hero?”
GlobalLogic Java Community Webinar #17 “SpringJDBC vs JDBC. Is Spring a Hero?”GlobalLogic Ukraine
 
GlobalLogic JavaScript Community Webinar #18 “Long Story Short: OSI Model”
GlobalLogic JavaScript Community Webinar #18 “Long Story Short: OSI Model”GlobalLogic JavaScript Community Webinar #18 “Long Story Short: OSI Model”
GlobalLogic JavaScript Community Webinar #18 “Long Story Short: OSI Model”GlobalLogic Ukraine
 
Штучний інтелект як допомога в навчанні, а не замінник.pptx
Штучний інтелект як допомога в навчанні, а не замінник.pptxШтучний інтелект як допомога в навчанні, а не замінник.pptx
Штучний інтелект як допомога в навчанні, а не замінник.pptxGlobalLogic Ukraine
 
Задачі AI-розробника як застосовується штучний інтелект.pptx
Задачі AI-розробника як застосовується штучний інтелект.pptxЗадачі AI-розробника як застосовується штучний інтелект.pptx
Задачі AI-розробника як застосовується штучний інтелект.pptxGlobalLogic Ukraine
 
Що треба вивчати, щоб стати розробником штучного інтелекту та нейромереж.pptx
Що треба вивчати, щоб стати розробником штучного інтелекту та нейромереж.pptxЩо треба вивчати, щоб стати розробником штучного інтелекту та нейромереж.pptx
Що треба вивчати, щоб стати розробником штучного інтелекту та нейромереж.pptxGlobalLogic Ukraine
 
GlobalLogic Java Community Webinar #16 “Zaloni’s Architecture for Data-Driven...
GlobalLogic Java Community Webinar #16 “Zaloni’s Architecture for Data-Driven...GlobalLogic Java Community Webinar #16 “Zaloni’s Architecture for Data-Driven...
GlobalLogic Java Community Webinar #16 “Zaloni’s Architecture for Data-Driven...GlobalLogic Ukraine
 
JavaScript Community Webinar #14 "Why Is Git Rebase?"
JavaScript Community Webinar #14 "Why Is Git Rebase?"JavaScript Community Webinar #14 "Why Is Git Rebase?"
JavaScript Community Webinar #14 "Why Is Git Rebase?"GlobalLogic Ukraine
 
GlobalLogic .NET Community Webinar #3 "Exploring Serverless with Azure Functi...
GlobalLogic .NET Community Webinar #3 "Exploring Serverless with Azure Functi...GlobalLogic .NET Community Webinar #3 "Exploring Serverless with Azure Functi...
GlobalLogic .NET Community Webinar #3 "Exploring Serverless with Azure Functi...GlobalLogic Ukraine
 
Страх і сила помилок - IT Inside від GlobalLogic Education
Страх і сила помилок - IT Inside від GlobalLogic EducationСтрах і сила помилок - IT Inside від GlobalLogic Education
Страх і сила помилок - IT Inside від GlobalLogic EducationGlobalLogic Ukraine
 
GlobalLogic .NET Webinar #2 “Azure RBAC and Managed Identity”
GlobalLogic .NET Webinar #2 “Azure RBAC and Managed Identity”GlobalLogic .NET Webinar #2 “Azure RBAC and Managed Identity”
GlobalLogic .NET Webinar #2 “Azure RBAC and Managed Identity”GlobalLogic Ukraine
 
GlobalLogic QA Webinar “What does it take to become a Test Engineer”
GlobalLogic QA Webinar “What does it take to become a Test Engineer”GlobalLogic QA Webinar “What does it take to become a Test Engineer”
GlobalLogic QA Webinar “What does it take to become a Test Engineer”GlobalLogic Ukraine
 
“How to Secure Your Applications With a Keycloak?
“How to Secure Your Applications With a Keycloak?“How to Secure Your Applications With a Keycloak?
“How to Secure Your Applications With a Keycloak?GlobalLogic Ukraine
 
GlobalLogic Machine Learning Webinar “Advanced Statistical Methods for Linear...
GlobalLogic Machine Learning Webinar “Advanced Statistical Methods for Linear...GlobalLogic Machine Learning Webinar “Advanced Statistical Methods for Linear...
GlobalLogic Machine Learning Webinar “Advanced Statistical Methods for Linear...GlobalLogic Ukraine
 
GlobalLogic Machine Learning Webinar “Statistical learning of linear regressi...
GlobalLogic Machine Learning Webinar “Statistical learning of linear regressi...GlobalLogic Machine Learning Webinar “Statistical learning of linear regressi...
GlobalLogic Machine Learning Webinar “Statistical learning of linear regressi...GlobalLogic Ukraine
 
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”GlobalLogic Ukraine
 
Embedded Webinar #17 "Low-level Network Testing in Embedded Devices Development"
Embedded Webinar #17 "Low-level Network Testing in Embedded Devices Development"Embedded Webinar #17 "Low-level Network Testing in Embedded Devices Development"
Embedded Webinar #17 "Low-level Network Testing in Embedded Devices Development"GlobalLogic Ukraine
 
GlobalLogic Webinar "Introduction to Embedded QA"
GlobalLogic Webinar "Introduction to Embedded QA"GlobalLogic Webinar "Introduction to Embedded QA"
GlobalLogic Webinar "Introduction to Embedded QA"GlobalLogic Ukraine
 
C++ Webinar "Why Should You Learn C++ in 2021-22?"
C++ Webinar "Why Should You Learn C++ in 2021-22?"C++ Webinar "Why Should You Learn C++ in 2021-22?"
C++ Webinar "Why Should You Learn C++ in 2021-22?"GlobalLogic Ukraine
 
GlobalLogic Test Automation Live Testing Session “Android Behind UI — Testing...
GlobalLogic Test Automation Live Testing Session “Android Behind UI — Testing...GlobalLogic Test Automation Live Testing Session “Android Behind UI — Testing...
GlobalLogic Test Automation Live Testing Session “Android Behind UI — Testing...GlobalLogic Ukraine
 
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...GlobalLogic Ukraine
 

Más de GlobalLogic Ukraine (20)

GlobalLogic Java Community Webinar #17 “SpringJDBC vs JDBC. Is Spring a Hero?”
GlobalLogic Java Community Webinar #17 “SpringJDBC vs JDBC. Is Spring a Hero?”GlobalLogic Java Community Webinar #17 “SpringJDBC vs JDBC. Is Spring a Hero?”
GlobalLogic Java Community Webinar #17 “SpringJDBC vs JDBC. Is Spring a Hero?”
 
GlobalLogic JavaScript Community Webinar #18 “Long Story Short: OSI Model”
GlobalLogic JavaScript Community Webinar #18 “Long Story Short: OSI Model”GlobalLogic JavaScript Community Webinar #18 “Long Story Short: OSI Model”
GlobalLogic JavaScript Community Webinar #18 “Long Story Short: OSI Model”
 
Штучний інтелект як допомога в навчанні, а не замінник.pptx
Штучний інтелект як допомога в навчанні, а не замінник.pptxШтучний інтелект як допомога в навчанні, а не замінник.pptx
Штучний інтелект як допомога в навчанні, а не замінник.pptx
 
Задачі AI-розробника як застосовується штучний інтелект.pptx
Задачі AI-розробника як застосовується штучний інтелект.pptxЗадачі AI-розробника як застосовується штучний інтелект.pptx
Задачі AI-розробника як застосовується штучний інтелект.pptx
 
Що треба вивчати, щоб стати розробником штучного інтелекту та нейромереж.pptx
Що треба вивчати, щоб стати розробником штучного інтелекту та нейромереж.pptxЩо треба вивчати, щоб стати розробником штучного інтелекту та нейромереж.pptx
Що треба вивчати, щоб стати розробником штучного інтелекту та нейромереж.pptx
 
GlobalLogic Java Community Webinar #16 “Zaloni’s Architecture for Data-Driven...
GlobalLogic Java Community Webinar #16 “Zaloni’s Architecture for Data-Driven...GlobalLogic Java Community Webinar #16 “Zaloni’s Architecture for Data-Driven...
GlobalLogic Java Community Webinar #16 “Zaloni’s Architecture for Data-Driven...
 
JavaScript Community Webinar #14 "Why Is Git Rebase?"
JavaScript Community Webinar #14 "Why Is Git Rebase?"JavaScript Community Webinar #14 "Why Is Git Rebase?"
JavaScript Community Webinar #14 "Why Is Git Rebase?"
 
GlobalLogic .NET Community Webinar #3 "Exploring Serverless with Azure Functi...
GlobalLogic .NET Community Webinar #3 "Exploring Serverless with Azure Functi...GlobalLogic .NET Community Webinar #3 "Exploring Serverless with Azure Functi...
GlobalLogic .NET Community Webinar #3 "Exploring Serverless with Azure Functi...
 
Страх і сила помилок - IT Inside від GlobalLogic Education
Страх і сила помилок - IT Inside від GlobalLogic EducationСтрах і сила помилок - IT Inside від GlobalLogic Education
Страх і сила помилок - IT Inside від GlobalLogic Education
 
GlobalLogic .NET Webinar #2 “Azure RBAC and Managed Identity”
GlobalLogic .NET Webinar #2 “Azure RBAC and Managed Identity”GlobalLogic .NET Webinar #2 “Azure RBAC and Managed Identity”
GlobalLogic .NET Webinar #2 “Azure RBAC and Managed Identity”
 
GlobalLogic QA Webinar “What does it take to become a Test Engineer”
GlobalLogic QA Webinar “What does it take to become a Test Engineer”GlobalLogic QA Webinar “What does it take to become a Test Engineer”
GlobalLogic QA Webinar “What does it take to become a Test Engineer”
 
“How to Secure Your Applications With a Keycloak?
“How to Secure Your Applications With a Keycloak?“How to Secure Your Applications With a Keycloak?
“How to Secure Your Applications With a Keycloak?
 
GlobalLogic Machine Learning Webinar “Advanced Statistical Methods for Linear...
GlobalLogic Machine Learning Webinar “Advanced Statistical Methods for Linear...GlobalLogic Machine Learning Webinar “Advanced Statistical Methods for Linear...
GlobalLogic Machine Learning Webinar “Advanced Statistical Methods for Linear...
 
GlobalLogic Machine Learning Webinar “Statistical learning of linear regressi...
GlobalLogic Machine Learning Webinar “Statistical learning of linear regressi...GlobalLogic Machine Learning Webinar “Statistical learning of linear regressi...
GlobalLogic Machine Learning Webinar “Statistical learning of linear regressi...
 
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
GlobalLogic C++ Webinar “The Minimum Knowledge to Become a C++ Developer”
 
Embedded Webinar #17 "Low-level Network Testing in Embedded Devices Development"
Embedded Webinar #17 "Low-level Network Testing in Embedded Devices Development"Embedded Webinar #17 "Low-level Network Testing in Embedded Devices Development"
Embedded Webinar #17 "Low-level Network Testing in Embedded Devices Development"
 
GlobalLogic Webinar "Introduction to Embedded QA"
GlobalLogic Webinar "Introduction to Embedded QA"GlobalLogic Webinar "Introduction to Embedded QA"
GlobalLogic Webinar "Introduction to Embedded QA"
 
C++ Webinar "Why Should You Learn C++ in 2021-22?"
C++ Webinar "Why Should You Learn C++ in 2021-22?"C++ Webinar "Why Should You Learn C++ in 2021-22?"
C++ Webinar "Why Should You Learn C++ in 2021-22?"
 
GlobalLogic Test Automation Live Testing Session “Android Behind UI — Testing...
GlobalLogic Test Automation Live Testing Session “Android Behind UI — Testing...GlobalLogic Test Automation Live Testing Session “Android Behind UI — Testing...
GlobalLogic Test Automation Live Testing Session “Android Behind UI — Testing...
 
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
 

Último

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 

Último (20)

Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 

Pattern Matching in Java 14

  • 1. 1 Java 14 Pattern Matching Oleksandr Navka Lead Software Engineer, Consultant, GlobalLogic
  • 2. 2 1. Updated instanceof 2. Updated switch-case 3. Java Records Agenda
  • 3. 3 Тагир Валеев JetBrains Pattern matching и его воображаемые друзья
  • 4. 4
  • 5. 5 When someone match something :) What is pattern matching?
  • 6. 6 Any pattern • _ Constant pattern • 1 • true • null Type pattern • String s Var pattern • var i = 1 Deconstruction pattern • Optional(String s) • Node(Node left, Node right) What are the patterns?
  • 8. 8 public class Animal { } class Dog extends Animal { public void bark() { } } class Cat extends Animal { public void meow() { } }
  • 9. 9 public static void say(Animal animal) { if (animal instanceof Dog) { Dog dog = (Dog) animal; dog.bark(); } else if (animal instanceof Cat) { Cat cat = (Cat) animal; cat.meow(); } }
  • 10. 10 public static void say(Animal animal) { if (animal instanceof Dog) { Dog dog = (Dog) animal; dog.bark(); } else if (animal instanceof Cat) { Cat cat = (Cat) animal; cat.meow(); } }
  • 11. 11 public static void say(Animal animal) { if (animal instanceof Dog dog) { dog.bark(); } else if (animal instanceof Cat cat) { cat.meow(); } }
  • 12. 12 public static void say(Animal animal) { if (animal instanceof Dog dog) { dog.bark(); } else if (animal instanceof Cat cat) { cat.meow(); } }
  • 15. 15 if (obj instanceof String s) { // can use s here } else { // can't use s here }
  • 16. 16 if (obj instanceof String s) { // can use s here } else { // can't use s here } binding variable
  • 17. 17 if (!(obj instanceof String s)) { } else { s.contains("a") }
  • 18. 18 if(obj instanceof String s && s.length() > 5) { System.out.println(s.toUpperCase()); }
  • 19. 19 if(obj instanceof String s || s.length() > 5) { System.out.println(s.toUpperCase()); }
  • 20. 20 if(!(obj instanceof String s) || s.length() > 5) { System.out.println("Do something"); } else { System.out.println(s.toUpperCase()); }
  • 21. 21 if(!(obj instanceof String s) || s.length() > 5) { System.out.println("Do something"); } else { System.out.println(s.toUpperCase()); }
  • 22. 22 if(!(obj instanceof String s) || s.length() > 5) { throw new IllegalArgumentException(); } System.out.println(s.toUpperCase());
  • 23. 23 if(!(obj instanceof String s) || s.length() > 5) { throw new IllegalArgumentException(); } System.out.println(s.toUpperCase());
  • 24. 24 if (!(obj instanceof String s)) { s.contains(..) } else { s.contains(..) }
  • 25. 25 private String s = "abc"; if (!(obj instanceof String s)) { s.contains(..) } else { s.contains(..) }
  • 26. 26 1. if (obj instanceof String s) {s.trim()} 2. if (!(obj instanceof String s)) {} else {s.contains("a")} 3. if(obj instanceof String s && s.length() > 5) {} 4. if(obj instanceof String s || s.length() > 5) {} Binding variable scope
  • 27. 27 1. if (obj instanceof String s) {s.trim()} 2. if (!(obj instanceof String s)) {} else {s.contains("a")} 3. if(obj instanceof String s && s.length() > 5) {} 4. if(obj instanceof String s || s.length() > 5) {} Binding variable scope
  • 28. 28 1. if (obj instanceof String s) {s.trim()} 2. if (!(obj instanceof String s)) {} else {s.contains("a")} 3. if(obj instanceof String s && s.length() > 5) {} 4. if(obj instanceof String s || s.length() > 5) {} 5. if(!(obj instanceof String s) || s.length() > 5) {} 6. if(!(obj instanceof String s) || s.length() > 5) {} else {s.toUpperCase());} 7. if(!(obj instanceof String s) || s.length() > 5) { throw new IllegalArgumentException(); } System.out.println(s.toUpperCase()); Binding variable scope
  • 29. 29 1. instanceof has got type pattern matching 2. instanceof the first java feature that got pattern matching 3. Be aware about scoping Instanceof Summary
  • 31. 31 public enum ProgrammingParadigm {OBJECT_ORIENTED, PROCEDURAL, FUNCTIONAL} public static List<String> getProgrammingLanguage(ProgrammingParadigm programmingParadigm) { List<String> languages = new ArrayList<>(); switch (programmingParadigm) { case OBJECT_ORIENTED: languages.add("Java"); languages.add("C++"); break; case FUNCTIONAL: languages.add("Haskel"); break; case PROCEDURAL: languages.add("Pascal"); break; } return languages; } Old version of switch
  • 32. 32 public enum ProgrammingParadigm {OBJECT_ORIENTED, PROCEDURAL, FUNCTIONAL} public static List<String> getProgrammingLanguage(ProgrammingParadigm programmingParadigm) { List<String> languages = new ArrayList<>(); switch (programmingParadigm) { case OBJECT_ORIENTED: languages.add("Java"); languages.add("C++"); break; case FUNCTIONAL: languages.add("Haskel"); break; case PROCEDURAL: languages.add("Pascal"); break; } return languages; } Old version of switch
  • 33. 33 public enum ProgrammingParadigm {OBJECT_ORIENTED, PROCEDURAL, FUNCTIONAL} public static List<String> getProgrammingLanguage(ProgrammingParadigm programmingParadigm) { List<String> languages = new ArrayList<>(); switch (programmingParadigm) { case OBJECT_ORIENTED: languages.add("Java"); languages.add("C++"); break; case FUNCTIONAL: languages.add("Haskel"); break; case PROCEDURAL: languages.add("Pascal"); break; } return languages; } Old version of switch Input type limitations: byte, short, char, and int Enum Types, String
  • 34. 34 public enum ProgrammingParadigm {OBJECT_ORIENTED, PROCEDURAL, FUNCTIONAL} public static List<String> getProgrammingLanguage(ProgrammingParadigm programmingParadigm) { List<String> languages = new ArrayList<>(); switch (programmingParadigm) { case OBJECT_ORIENTED: languages.add("Java"); languages.add("C++"); break; case FUNCTIONAL: languages.add("Haskel"); break; case PROCEDURAL: languages.add("Pascal"); break; } return languages; } Old version of switch Can’t get result
  • 35. 35 public static List<String> getProgrammingLanguage(ProgrammingParadigm programmingParadigm) { switch (programmingParadigm) { case OBJECT_ORIENTED: return List.of("Java", "C++"); case FUNCTIONAL: return List.of("Haskel"); case PROCEDURAL: return List.of("Pascal"); default: return List.of(); } }
  • 36. 36 public static List<String> getProgrammingLanguage(ProgrammingParadigm programmingParadigm) { switch (programmingParadigm) { case OBJECT_ORIENTED: return List.of("Java", "C++"); case FUNCTIONAL: return List.of("Haskel"); case PROCEDURAL: return List.of("Pascal"); default: return List.of(); } }
  • 37. 37 Disadvantages: 1. Always remember about break 2. Input data type limitation 3. Can’t get result 4. Don’t understand all possible options for enum Old version of switch
  • 39. 39 public void printObject(Object obj) { switch (obj) { case String s: System.out.println("This is String" + s.trim()); break; case Integer i: System.out.println("This is Integer" + i); break; case Number n: System.out.println("This is number"+ n); break; default: System.out.println("I don't know"); } }
  • 40. 40 public void printObject(Object obj) { switch (obj) { case String s: System.out.println("This is String" + s.trim()); break; case Number n: System.out.println("This is number"+ n); break; case Integer i: System.out.println("This is Integer" + i); break; default: System.out.println("I don't know"); } }
  • 41. 41 public void printObject(Object obj) { switch (obj) { case String s: System.out.println("This is String" + s.trim()); break; default: System.out.println("I don't know"); case Number n: System.out.println("This is number"+ n); break; case Integer i: System.out.println("This is Integer" + i); break; } }
  • 43. 43 public static List<String> getProgrammingLanguage(ProgrammingParadigm programmingParadigm) { return switch (programmingParadigm) { case OBJECT_ORIENTED -> List.of("Java", "C++"); case FUNCTIONAL -> List.of("Haskel"); case PROCEDURAL -> List.of("Pascal"); }; } Updated switch case
  • 44. 44 public static List<String> getProgrammingLanguage(ProgrammingParadigm programmingParadigm) { return switch (programmingParadigm) { case OBJECT_ORIENTED -> { System.out.println("You are master of polymorphism"); yield List.of("Java", "C++"); } case FUNCTIONAL -> { System.out.println("You are master of recursion"); yield List.of("Haskel"); } case PROCEDURAL -> { System.out.println("Try to use another paradigms"); yield List.of("Pascal"); } }; }
  • 45. 45 1. Don’t need to use break 2. Can return result 3. Don’t need to use default branch if all options described 4. Input data types have limitations Summary of updated switch
  • 47. 47 private record Car(int speed, String manufacturer) { } Java Records
  • 48. 48 private record Car(int speed, String manufacturer) { } public static void main(String[] args) { Car car = new Car(100, "Mercedes"); System.out.println(car.speed()); System.out.println(car.manufacturer()); } Java Records
  • 49. 49 private record Car(int speed, String manufacturer) { } public static void main(String[] args) { Car car = new Car(100, "Mercedes"); System.out.println(car); } ------- Output: Car[speed=100, manufacturer=Mercedes] Java Records
  • 50. 50 private static final class Car extends java.lang.Record { private final int speed; private final java.lang.String manufacturer; public Car(int speed, java.lang.String manufacturer) { /* compiled code */ } public java.lang.String toString() { /* compiled code */ } public final int hashCode() { /* compiled code */ } public final boolean equals(java.lang.Object o) { /* compiled code */ } public int speed() { /* compiled code */ } public java.lang.String manufacturer() { /* compiled code */ } }
  • 51. 51 private static final class Car extends java.lang.Record { private final int speed; private final java.lang.String manufacturer; public Car(int speed, java.lang.String manufacturer) { /* compiled code */ } public java.lang.String toString() { /* compiled code */ } public final int hashCode() { /* compiled code */ } public final boolean equals(java.lang.Object o) { /* compiled code */ } public int speed() { /* compiled code */ } public java.lang.String manufacturer() { /* compiled code */ } }
  • 52. 52 public abstract class Record { protected Record() {} @Override public abstract boolean equals(Object obj); @Override public abstract int hashCode(); @Override public abstract String toString(); }
  • 53. 53 private static final class Car extends java.lang.Record { private final int speed; private final java.lang.String manufacturer; public Car(int speed, java.lang.String manufacturer) { /* compiled code */ } public java.lang.String toString() { /* compiled code */ } public final int hashCode() { /* compiled code */ } public final boolean equals(java.lang.Object o) { /* compiled code */ } public int speed() { /* compiled code */ } public java.lang.String manufacturer() { /* compiled code */ } }
  • 54. 54 private static final class Car extends java.lang.Record { private final int speed; private final java.lang.String manufacturer; public Car(int speed, java.lang.String manufacturer) { /* compiled code */ } public java.lang.String toString() { /* compiled code */ } public final int hashCode() { /* compiled code */ } public final boolean equals(java.lang.Object o) { /* compiled code */ } public int speed() { /* compiled code */ } public java.lang.String manufacturer() { /* compiled code */ } }
  • 55. 55 private record Car(int speed, String manufacturer) { public Car { if(speed < 0) { throw new IllegalArgumentException(); } } } public static void main(String[] args) { var car = new Car(-2, "Mercedes"); System.out.println(car); } Canonical constructor
  • 56. 56 private record Car(int speed, String manufacturer) { public Car { if(speed < 0) { throw new IllegalArgumentException(); } } public Car(String manufacturer) { this(0, manufacturer); } } Canonical constructor
  • 57. 57 private static final class Car extends java.lang.Record { private final int speed; private final java.lang.String manufacturer; public Car(int speed, java.lang.String manufacturer) { /* compiled code */ } public java.lang.String toString() { /* compiled code */ } public final int hashCode() { /* compiled code */ } public final boolean equals(java.lang.Object o) { /* compiled code */ } public int speed() { /* compiled code */ } public java.lang.String manufacturer() { /* compiled code */ } }
  • 58. 58 private static final class Car extends java.lang.Record { private final int speed; private final java.lang.String manufacturer; public Car(int speed, java.lang.String manufacturer) { /* compiled code */ } public java.lang.String toString() { /* compiled code */ } public final int hashCode() { /* compiled code */ } public final boolean equals(java.lang.Object o) { /* compiled code */ } public int speed() { /* compiled code */ } public java.lang.String manufacturer() { /* compiled code */ } }
  • 59. 59 1. Java 14 present first version of pattern matching 2. Type pattern matching present in instanceof operator 3. New Switch statement was released with some blanks for pattern matching 4. Keep eye on Java Records and deconstruction patterns Summary