├── .gitignore ├── Java_14_Features.pdf ├── Java_15_Features.pdf ├── README.adoc ├── pom.xml └── src └── main └── java └── com └── kodedu ├── instanceofs ├── InstanceOfApp1.java └── InstanceOfApp2.java ├── nullpointerexception └── NullPointerApp.java ├── records ├── Point.java ├── RecordsApp1.java └── RecordsApp2.java ├── seals ├── SealedApp.java └── products │ ├── ConstantExpr.java │ ├── Expr.java │ ├── NegExpr.java │ ├── PlusExpr.java │ └── TimesExpr.java ├── switches ├── ClassicSwitchApp.java ├── SwitchApp1.java ├── SwitchApp2.java ├── SwitchApp3.java └── VehicleType.java └── texts ├── TextApp1.java ├── TextApp2.java ├── TextApp3.java ├── TextApp4.java ├── TextApp5.java ├── TextApp6.java ├── TextApp7.java └── TextUtil.java /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | *.iml 3 | /target -------------------------------------------------------------------------------- /Java_14_Features.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahmanusta/java15-edu/7ccab6d6ff36a39b396966b4d4d7f2c0398c7da7/Java_14_Features.pdf -------------------------------------------------------------------------------- /Java_15_Features.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rahmanusta/java15-edu/7ccab6d6ff36a39b396966b4d4d7f2c0398c7da7/Java_15_Features.pdf -------------------------------------------------------------------------------- /README.adoc: -------------------------------------------------------------------------------- 1 | == Java 15 Features 2 | 3 | This repository includes some selected Java 15 features which have presented in conference and meetups. 4 | 5 | == Notes 6 | 7 | * Be sure `--enable-preview` option is enabled to run preview features 8 | //* Be sure `-XX:+ShowCodeDetailsInExceptionMessages` option is enabled to see helpful NLP output 9 | 10 | == Slides 11 | 12 | You can find slides here link:Java_15_Features.pdf[] 13 | 14 | == How to run 15 | 16 | Download JDK 15 https://jdk.java.net/15/ and use your favorite IDE which supports Java 14. 17 | 18 | or you can try features on https://tryjshell.org/ 19 | 20 | == Author 21 | 22 | Rahman Usta -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 4.0.0 6 | 7 | com.kodedu 8 | java15-edu 9 | 1.0-SNAPSHOT 10 | 11 | 12 | 13 | 14 | org.apache.maven.plugins 15 | maven-compiler-plugin 16 | 3.8.1 17 | 18 | 19 | --enable-preview 20 | 21 | 15 22 | 15 23 | 24 | 25 | 26 | 27 | 28 | 29 | UTF-8 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/instanceofs/InstanceOfApp1.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.instanceofs; 2 | 3 | public class InstanceOfApp1 { 4 | 5 | public static void main(String[] args) { 6 | 7 | Object obj = "Hello world!"; 8 | 9 | // before 10 | if (obj instanceof String) { 11 | String s = (String) obj; 12 | System.out.println("String: " + s); 13 | } 14 | 15 | // after 16 | if (obj instanceof String s) { 17 | System.out.println("String: " + s); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/instanceofs/InstanceOfApp2.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.instanceofs; 2 | 3 | public class InstanceOfApp2 { 4 | 5 | public static void main(String[] args) { 6 | 7 | Object obj = "Hello world!"; 8 | 9 | // legal usage 10 | if (obj instanceof String s && !s.isBlank()) { 11 | System.out.println("String: " + s); 12 | } 13 | 14 | // cannot resolve symbol 's' 15 | // if (obj instanceof String s || !s.isBlank()) { 16 | // System.out.println("String: " + s); 17 | // } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/nullpointerexception/NullPointerApp.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.nullpointerexception; 2 | 3 | public class NullPointerApp { 4 | 5 | /** 6 | * Enable flag to see helpful null pointer exception message 7 | * -XX:+ShowCodeDetailsInExceptionMessages 8 | * 9 | * helpful NLP is enabled by default from JDK 15 10 | */ 11 | public static void main(String[] args) { 12 | 13 | Person person = new Person(); 14 | person.address = new Address(); 15 | String toUpperCase = person.address.street.toUpperCase(); 16 | System.out.println(toUpperCase); 17 | 18 | } 19 | 20 | static class Person { 21 | Address address; 22 | } 23 | 24 | static class Address { 25 | String street; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/records/Point.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.records; 2 | 3 | public record Point(int x, int y) { } -------------------------------------------------------------------------------- /src/main/java/com/kodedu/records/RecordsApp1.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.records; 2 | 3 | public class RecordsApp1 { 4 | 5 | public static void main(String[] args) { 6 | 7 | Point point = new Point(2, 3); 8 | 9 | System.out.println(point); 10 | 11 | int x = point.x(); 12 | int y = point.y(); 13 | System.out.format("X: %d, Y: %d", x, y); 14 | } 15 | } 16 | 17 | 18 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/records/RecordsApp2.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.records; 2 | 3 | public class RecordsApp2 { 4 | 5 | public static void main(String[] args) { 6 | 7 | Point point1 = new Point(2, 3); 8 | Point point2 = new Point(2, 3); 9 | 10 | System.out.println("Equal? " + point1.equals(point2)); 11 | } 12 | } 13 | 14 | 15 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/seals/SealedApp.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.seals; 2 | 3 | import com.kodedu.seals.products.*; 4 | 5 | public class SealedApp { 6 | 7 | public static void main(String[] args) { 8 | 9 | // y = x * 2 + 1 10 | 11 | var x = 3; 12 | int y = calculate( 13 | new PlusExpr( 14 | new TimesExpr( 15 | new ConstantExpr(x), 16 | new ConstantExpr(2) 17 | ), 18 | new ConstantExpr(1) 19 | ) 20 | ); 21 | System.out.println("Value of y: " + y); 22 | 23 | } 24 | 25 | private static int calculate(Expr expr) { 26 | if (expr instanceof ConstantExpr e) { 27 | return e.value(); 28 | } else if (expr instanceof PlusExpr e) { 29 | return calculate(e.first()) + calculate(e.second()); 30 | } else if (expr instanceof TimesExpr e) { 31 | return calculate(e.first()) * calculate(e.second()); 32 | } else if (expr instanceof NegExpr e) { 33 | return -calculate(e.expr()); 34 | } else { 35 | throw new RuntimeException("There is no such case " + expr); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/seals/products/ConstantExpr.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.seals.products; 2 | 3 | public record ConstantExpr(int value) implements Expr { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/seals/products/Expr.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.seals.products; 2 | 3 | public sealed interface Expr permits ConstantExpr, NegExpr, PlusExpr, TimesExpr { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/seals/products/NegExpr.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.seals.products; 2 | 3 | public record NegExpr(Expr expr) implements Expr { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/seals/products/PlusExpr.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.seals.products; 2 | 3 | public record PlusExpr(Expr first, Expr second) implements Expr { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/seals/products/TimesExpr.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.seals.products; 2 | 3 | public record TimesExpr(Expr first, Expr second) implements Expr { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/switches/ClassicSwitchApp.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.switches; 2 | 3 | public class ClassicSwitchApp { 4 | 5 | public static void main(String[] args) { 6 | VehicleType vehicleType = VehicleType.AUTOMOBILE; 7 | 8 | int speedLimit = -1; 9 | switch (vehicleType) { 10 | case BIKE: 11 | case SCOOTER: 12 | speedLimit = 40; 13 | break; 14 | case MOTORBIKE: 15 | case AUTOMOBILE: 16 | speedLimit = 140; 17 | break; 18 | case TRUCK: 19 | speedLimit = 80; 20 | break; 21 | } 22 | 23 | System.out.println("Speed limit: " + speedLimit); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/switches/SwitchApp1.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.switches; 2 | 3 | public class SwitchApp1 { 4 | 5 | public static void main(String[] args) { 6 | 7 | VehicleType vehicleType = VehicleType.AUTOMOBILE; 8 | 9 | int speedLimit = switch (vehicleType) { 10 | case BIKE, SCOOTER -> 40; 11 | case MOTORBIKE, AUTOMOBILE -> 140; 12 | case TRUCK -> 80; 13 | // default -> throw new IllegalStateException("No case found for: " + vehicleType); 14 | }; 15 | 16 | System.out.println("Speed limit: " + speedLimit); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/switches/SwitchApp2.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.switches; 2 | 3 | public class SwitchApp2 { 4 | 5 | public static void main(String[] args) { 6 | 7 | int speedLimit = getSpeedLimit(VehicleType.AUTOMOBILE); 8 | System.out.println("Speed limit: " + speedLimit); 9 | 10 | } 11 | 12 | private static int getSpeedLimit(VehicleType vehicleType) { 13 | return switch (vehicleType) { 14 | case BIKE, SCOOTER -> 40; 15 | case MOTORBIKE, AUTOMOBILE -> 140; 16 | case TRUCK -> 80; 17 | }; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/switches/SwitchApp3.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.switches; 2 | 3 | import java.util.concurrent.ThreadLocalRandom; 4 | 5 | public class SwitchApp3 { 6 | 7 | public static void main(String[] args) { 8 | 9 | VehicleType vehicleType = VehicleType.TRUCK; 10 | 11 | int speedLimit = switch (vehicleType) { 12 | case BIKE, SCOOTER -> 40; 13 | case MOTORBIKE, AUTOMOBILE -> 140; 14 | case TRUCK -> { 15 | int randomSpeed = ThreadLocalRandom.current().nextInt(70, 80); 16 | yield randomSpeed; 17 | } 18 | }; 19 | 20 | System.out.println("Speed limit: " + speedLimit); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/switches/VehicleType.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.switches; 2 | 3 | public enum VehicleType { 4 | BIKE, SCOOTER, 5 | MOTORBIKE, AUTOMOBILE, 6 | TRUCK 7 | } 8 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/texts/TextApp1.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.texts; 2 | 3 | import static com.kodedu.texts.TextUtil.printText; 4 | 5 | public class TextApp1 { 6 | 7 | public static void main(String[] args) { 8 | String html = 9 | """ 10 | 11 | 12 |

Hello, world

13 | 14 | 15 | """; 16 | 17 | printText(html); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/texts/TextApp2.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.texts; 2 | 3 | public class TextApp2 { 4 | 5 | public static void main(String[] args) { 6 | 7 | // Line terminator is required 8 | // equal to-> var text = "" 9 | var text = """ 10 | """; 11 | 12 | // var text2 = """"""; // illegal text block start 13 | // var text3 = """ """; // illegal text block start 14 | 15 | TextUtil.printText(text); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/texts/TextApp3.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.texts; 2 | 3 | import static com.kodedu.texts.TextUtil.printText; 4 | 5 | public class TextApp3 { 6 | 7 | public static void main(String[] args) { 8 | String html = """ 9 | 10 | 11 |

Hello, world

12 | 13 | 14 | """; 15 | 16 | printText(html); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/texts/TextApp4.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.texts; 2 | 3 | import static com.kodedu.texts.TextUtil.printText; 4 | 5 | public class TextApp4 { 6 | 7 | public static void main(String[] args) { 8 | String html = """ 9 | 10 | 11 |

Hello, world

12 | 13 | 14 | """; 15 | 16 | printText(html); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/texts/TextApp5.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.texts; 2 | 3 | import static com.kodedu.texts.TextUtil.printText; 4 | 5 | public class TextApp5 { 6 | 7 | public static void main(String[] args) { 8 | String html = """ 9 | \ 10 | \ 11 |

Hello, world

\ 12 | \ 13 | \ 14 | """; 15 | 16 | printText(html); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/texts/TextApp6.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.texts; 2 | 3 | import static com.kodedu.texts.TextUtil.printText; 4 | 5 | public class TextApp6 { 6 | 7 | public static void main(String[] args) { 8 | String html = """ 9 | \s 10 | \s 11 |

Hello, world

\s 12 | \s 13 | \s 14 | """; 15 | printText(html); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/texts/TextApp7.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.texts; 2 | 3 | import static com.kodedu.texts.TextUtil.printText; 4 | 5 | public class TextApp7 { 6 | 7 | public static void main(String[] args) { 8 | String html = """ 9 | 10 | 11 |

%s, %s

12 | 13 | 14 | """; 15 | html = html.formatted("Hello", "world!"); 16 | 17 | printText(html); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/kodedu/texts/TextUtil.java: -------------------------------------------------------------------------------- 1 | package com.kodedu.texts; 2 | 3 | import java.util.concurrent.atomic.AtomicInteger; 4 | import java.util.function.Supplier; 5 | 6 | public class TextUtil { 7 | 8 | public static void printText(String string) { 9 | string = string.replaceAll(" ", "•"); 10 | string = string.replaceAll("\n", "⏎\n"); 11 | string = string.trim(); 12 | System.out.println(string.isBlank() ? "Blank" : string); 13 | } 14 | 15 | } 16 | --------------------------------------------------------------------------------