Java Cram Session: Get caught up on the latest Java changes

Java Cram Session: Get caught up on the latest Java changes

Java is moving a lot faster than it used to, and it can be hard for busy developers to keep up with all of the new features. But don’t worry! Come to this talk and we’ll go over everything you need to know to catch up with the latest Java developments. This talk will focus on when and how to use the new language changes starting with Java 9. After this talk, you’ll be ready to code with the latest and greatest Java features like a pro.

2777430c1f9869e411ab5d8b17d8ba2a?s=128

Todd Ginsberg

September 30, 2020
Tweet

Transcript

  1. Todd Ginsberg Principal Developer Netspend @toddginsberg Java Cram Session https://jconf.dev

  2. @ToddGinsberg Who Is This Talk For? • Java developers still

    on Java 8
  3. @ToddGinsberg Who Is This Talk For? • Java developers still

    on Java 8 • Are overwhelmed by all of the changes
  4. @ToddGinsberg What Are We Covering? • New Java syntax

  5. @ToddGinsberg What Are We Covering? • New Java syntax •

    Interesting new libraries
  6. @ToddGinsberg What Are We Covering? • New Java syntax •

    Interesting new libraries • Interesting new methods
  7. @ToddGinsberg What Are We Covering? • New Java syntax •

    Interesting new libraries • Interesting new methods • Strategies for upgrading and keeping up
  8. @ToddGinsberg What Aren’t We Covering? • Bytecode-level changes

  9. @ToddGinsberg What Aren’t We Covering? • Bytecode-level changes • Most

    deprecations
  10. @ToddGinsberg What Aren’t We Covering? • Bytecode-level changes • Most

    deprecations • New libraries and methods that you may not need right away
  11. @ToddGinsberg What Aren’t We Covering? • Bytecode-level changes • Most

    deprecations • New libraries and methods that you may not need right away • Licensing and Paid Support
  12. The State Of Things

  13. @ToddGinsberg Current State Java 14

  14. @ToddGinsberg Current State Java 14 Java 15 No Longer Supported

    Latest and Greatest
  15. @ToddGinsberg Current State Java 14 Java 15 Java 16 No

    Longer Supported Latest and Greatest Coming March 2021
  16. @ToddGinsberg No More Major Releases • Six month Feature Releases

    only
  17. @ToddGinsberg No More Major Releases • Six month Feature Releases

    only • Miss the train? Wait for the next one
  18. @ToddGinsberg No More Major Releases • Six month Feature Releases

    only • Miss the train? Wait for the next one • Allows for preview features
  19. @ToddGinsberg No More Major Releases • Six month Feature Releases

    only • Miss the train? Wait for the next one • Allows for preview features • Two quarterly patch releases
  20. @ToddGinsberg No More Major Releases • Six month Feature Releases

    only • Miss the train? Wait for the next one • Allows for preview features • Two quarterly patch releases • Upgrading is easier
  21. @ToddGinsberg Long Term Support Java 11 Java 17 Still Receiving

    Patches Next LTS
  22. Upgrade Strategies

  23. @ToddGinsberg Java Upgrade Strategies A Update to LTS (11) and

    stay there until the next LTS (17)
  24. @ToddGinsberg Java Upgrade Strategies A Update to LTS (11) and

    stay there until the next LTS (17) B Update to the latest release as soon as it comes out
  25. @ToddGinsberg Java Upgrade Strategies A Update to LTS (11) and

    stay there until the next LTS (17) B Update to the latest release as soon as it comes out C Stay on Java 8 forever and let your skills atrophy
  26. @ToddGinsberg Java Upgrade Strategies A Update to LTS (11) and

    stay there until the next LTS (17) B Update to the latest release as soon as it comes out
  27. @ToddGinsberg The Rest of the Talk 1 Changes up until

    Java 11
  28. @ToddGinsberg The Rest of the Talk 1 Changes up until

    Java 11 2 Changes after Java 11
  29. Welcome To Java 11

  30. @ToddGinsberg JPMS (Modules): The Basics • Create a module system…

  31. @ToddGinsberg JPMS (Modules): The Basics • Create a module system…

    • And then modularize the JDK itself…
  32. @ToddGinsberg JPMS (Modules): The Basics • Create a module system…

    • And then modularize the JDK itself… • And provide tooling so we can create our own modules...
  33. @ToddGinsberg JPMS (Modules): The Basics • Create a module system…

    • And then modularize the JDK itself… • And provide tooling so we can create our own modules... • And our own subset runtimes
  34. @ToddGinsberg JPMS (Modules): Recommendation • Don’t worry about it for

    now
  35. @ToddGinsberg JPMS (Modules): Recommendation • Don’t worry about it for

    now • Take advantage of the fact that it made the JVM more organized
  36. @ToddGinsberg JPMS (Modules): Recommendation • Don’t worry about it for

    now • Take advantage of the fact that it made the JVM more organized • Wait and see what your dependencies do
  37. @ToddGinsberg Where Did The JRE Go? • Java does not

    ship with a JRE any more
  38. @ToddGinsberg Where Did The JRE Go? • Java does not

    ship with a JRE any more • Because the JVM is modular, we can create our own task-focused JVMs!
  39. @ToddGinsberg Where Did The JRE Go? • Java does not

    ship with a JRE any more • Because the JVM is modular, we can create our own task-focused JVMs! • Even if we don’t use modules!
  40. @ToddGinsberg jlink + jdeps == Focused JDK MyApp.jar AppDependency1.jar AppDependency2.jar

  41. @ToddGinsberg jlink + jdeps == Focused JDK MyApp.jar AppDependency1.jar AppDependency2.jar

    jdeps
  42. @ToddGinsberg jlink + jdeps == Focused JDK MyApp.jar AppDependency1.jar AppDependency2.jar

    jdeps List of Modules
  43. @ToddGinsberg jlink + jdeps == Focused JDK MyApp.jar AppDependency1.jar AppDependency2.jar

    jdeps List of Modules jlink
  44. @ToddGinsberg jlink + jdeps == Focused JDK MyApp.jar AppDependency1.jar AppDependency2.jar

    jdeps List of Modules jlink JVM
  45. @ToddGinsberg Where Did JavaFX Go? • Removed from the JVM!

  46. @ToddGinsberg Where Did JavaFX Go? • Removed from the JVM!

    • Still maintained, don’t panic! https://openjfx.io/
  47. @ToddGinsberg Say Goodbye To...

  48. @ToddGinsberg Say Goodbye To... • Applets

  49. @ToddGinsberg Say Goodbye To... • Applets • Web Start /

    JNLP
  50. @ToddGinsberg New! Compact Strings • Java char takes up two

    bytes • All Strings are char x2 bytes in Java 8
  51. @ToddGinsberg New! Compact Strings • Java char takes up two

    bytes • All Strings are char x2 bytes in Java 8 0 1 2 3 4 5 6 7 T “Todd” == o d d
  52. @ToddGinsberg New! Compact Strings • Java char takes up two

    bytes • All Strings are char x2 bytes in Java 8 0 1 2 3 4 5 6 7 T “Todd” == o d d 0 1 2 3 T o d d
  53. @ToddGinsberg New Garbage Collector Options • G1GC is the new

    default
  54. @ToddGinsberg New Garbage Collector Options • G1GC is the new

    default • CMS is now deprecated
  55. @ToddGinsberg New Garbage Collector Options • G1GC is the new

    default • CMS is now deprecated • Epsilon (for testing)
  56. @ToddGinsberg New Garbage Collector Options • G1GC is the new

    default • CMS is now deprecated • Epsilon (for testing) • ZGC is experimental
  57. @ToddGinsberg Multi-Version Jars

  58. @ToddGinsberg JShell - A Java REPL

  59. @ToddGinsberg Run Single Files Directly

  60. @ToddGinsberg Syntax - Removal of Single Underscore _

  61. @ToddGinsberg Syntax - Removal of Single Underscore ಠ_ಠ

  62. @ToddGinsberg Syntax - Local var public void doSomething() { String

    food = "French Fries"; int quantity = 2; }
  63. @ToddGinsberg Syntax - Local var public void doSomething() { var

    food = "French Fries"; var quantity = 2; }
  64. @ToddGinsberg Syntax - Local var public void doSomething() { var

    food = "French Fries"; var quantity = 2; }
  65. @ToddGinsberg Syntax - Local var public void doSomething() { List<Item>

    items = new ArrayList<>(); }
  66. @ToddGinsberg Syntax - Local var public void doSomething() { var

    items = new ArrayList<Item>(); }
  67. @ToddGinsberg Syntax - Local var public void doSomething() { var

    items = new ArrayList<Item>(); for(var item : items) { // ... } }
  68. @ToddGinsberg Syntax - Local var public void doSomething() { try(var

    connection = getConnection()) { // ... } }
  69. @ToddGinsberg Syntax - Local var public void doSomething() { //

    Yes var connection = getConnection(); var now = LocalTime.now(); }
  70. @ToddGinsberg Syntax - Private Interface Methods interface Something { private

    void somePrivateMethod() { // ... } }
  71. @ToddGinsberg Syntax - Private Interface Methods interface Something { private

    static void somePrivateMethod() { // ... } }
  72. @ToddGinsberg Library - Unmodifiable Collection Shortcuts List<String> list = List.of("A",

    "B", "C");
  73. @ToddGinsberg Library - Unmodifiable Collection Shortcuts List<String> list = List.of("A",

    "B", "C"); Set<String> set = Set.of("A", "B", "C");
  74. @ToddGinsberg Library - Unmodifiable Collection Shortcuts List<String> list = List.of("A",

    "B", "C"); Set<String> set = Set.of("A", "B", "C"); Map<String, Integer> map = Map.of("A", 1, "B", 2);
  75. @ToddGinsberg Library - Unmodifiable Collection Shortcuts var list = List.of("A",

    "B", "C"); var set = Set.of("A", "B", "C"); var map = Map.of("A", 1, "B", 2);
  76. @ToddGinsberg Library - Stream Enhancements numbers // [0,1,2,3,2,1,0] .stream() .takeWhile(it

    -> it < 3) // [0,1,2]
  77. @ToddGinsberg Library - Stream Enhancements numbers // [0,1,2,3,2,1,0] .stream() .dropWhile(it

    -> it < 3) // [3,2,1,0]
  78. @ToddGinsberg Library - Stream Enhancements numbers .stream() .dropWhile(it -> it

    < 3) .collect(Collectors.toUnmodifiableList());
  79. @ToddGinsberg Library - Stream Enhancements numbers .stream() .dropWhile(it -> it

    < 3) .collect(Collectors.toUnmodifiableSet());
  80. @ToddGinsberg Library - String Enhancements String sample = "1\n2\n3";

  81. @ToddGinsberg Library - String Enhancements String sample = "1\n2\n3"; sample

    .lines() // Stream<String> [1, 2, 3]
  82. @ToddGinsberg Library - String Enhancements String sample = "1\n2\n3"; sample

    .lines() // Stream<String> [1, 2, 3] .forEach(System.out::println);
  83. @ToddGinsberg Library - String Enhancements String sample = " sample

    ";
  84. @ToddGinsberg Library - String Enhancements String sample = " sample

    "; sample.strip() // "sample"
  85. @ToddGinsberg Library - String Enhancements String sample = " sample

    "; sample.strip() // "sample" sample.stripLeading() // "sample "
  86. @ToddGinsberg Library - String Enhancements String sample = " sample

    "; sample.strip() // "sample" sample.stripLeading() // "sample " sample.stripTrailing() // " sample"
  87. @ToddGinsberg Library - String Enhancements someString.isBlank()

  88. @ToddGinsberg Library - String Enhancements String yell = "Kh" +

    "a".repeat(5) + "n!"; // "Khaaaaan!"
  89. @ToddGinsberg Library - Collection.toArray(IntFunction) // In Collection.java: default <T> T[]

    toArray(IntFunction<T[]> generator)
  90. @ToddGinsberg Library - Collection.toArray(IntFunction) // In Collection.java: default <T> T[]

    toArray(IntFunction<T[]> generator) String[] strings = List .of("A", "B", "C") .toArray(String[]::new)
  91. @ToddGinsberg Library - Enhanced Deprecation @Deprecated

  92. @ToddGinsberg Library - Enhanced Deprecation @Deprecated(since = "9", forRemoval =

    true)
  93. @ToddGinsberg Library - HttpClient var client = HttpClient.newBuilder() .connectTimeout(Duration.ofSeconds(15)) .version(HttpClient.Version.HTTP_1_1)

    .build();
  94. @ToddGinsberg Library - HttpClient var request = HttpRequest.newBuilder() .GET() .uri(URI.create("https://frinkiac.com/api/random"))

    .build();
  95. @ToddGinsberg Library - HttpClient String quote = client .send(request, HttpResponse.BodyHandlers.ofString())

    .body();
  96. Welcome To Java 15

  97. @ToddGinsberg Say Goodbye To...

  98. @ToddGinsberg Say Goodbye To... • Nashorn Javascript Engine (and tools)

  99. @ToddGinsberg Say Goodbye To... • Nashorn Javascript Engine (and tools)

    • CMS GC
  100. @ToddGinsberg New Garbage Collector Options • ZGC • Shenandoah

  101. @ToddGinsberg Better NullPointerException Messages customer.getAddress().getStreet().toUpperCase();

  102. @ToddGinsberg Better NullPointerException Messages customer.getAddress().getStreet().toUpperCase(); Exception in thread "main" java.lang.NullPointerException

    at Yikes.main(Yikes.java:42)
  103. @ToddGinsberg Better NullPointerException Messages customer.getAddress().getStreet().toUpperCase(); -XX:+ShowCodeDetailsInExceptionMessages

  104. @ToddGinsberg Better NullPointerException Messages customer.getAddress().getStreet().toUpperCase(); Exception in thread "main" java.lang.NullPointerException:

    Cannot invoke "String.toUpperCase()" because the return value of "Address.getStreet()" is null at Yikes.main(Yikes.java:42)
  105. @ToddGinsberg Better NullPointerException Messages customer.getAddress().getStreet().toUpperCase(); Exception in thread "main" java.lang.NullPointerException:

    Cannot invoke "String.toUpperCase()" because the return value of "Address.getStreet()" is null at Yikes.main(Yikes.java:42)
  106. @ToddGinsberg Syntax - Text Blocks String json = "{\n" +

    " \"food\": \"French Fries\",\n" + " \"quantity\": 2\n" + "}";
  107. @ToddGinsberg Syntax - Text Blocks String json = """ {

    "food": "French Fries", "quantity: 2 } """;
  108. @ToddGinsberg Syntax - Switch Expressions String food = null; switch

    (id) { case 1: { food = "French Fries"; break; } case 2: { food = "Doughnuts"; break; } default: { food = "Pizza"; } }
  109. @ToddGinsberg Syntax - Switch Expressions String food = switch (id)

    { case 1 -> "French Fries"; case 2 -> "Doughnuts"; default -> "Pizza"; };
  110. @ToddGinsberg Syntax - Switch Expressions String food = switch (id)

    { case 1: { // Some other work here yield "French Fries"; } case 2: yield "Doughnuts"; default: yield "Pizza"; };
  111. @ToddGinsberg Good News - Enum Limits Increased!

  112. @ToddGinsberg Good News - Enum Limits Increased! ~2740

  113. @ToddGinsberg Good News - Enum Limits Increased! ~2740 ~4100

  114. Here Be Dragons

  115. @ToddGinsberg Syntax - instanceof Pattern Matching (Preview) if(someObject instanceof String)

    { String someString = (String)someObject; doSomethingWithString(someString); }
  116. @ToddGinsberg Syntax - instanceof Pattern Matching (Preview) if(someObject instanceof String

    someString) { doSomethingWithString(someString); }
  117. @ToddGinsberg Records (Preview) If we wanted an immutable 2-D Point

    we would need to:
  118. @ToddGinsberg Records (Preview) If we wanted an immutable 2-D Point

    we would need to: • Define a class
  119. @ToddGinsberg Records (Preview) If we wanted an immutable 2-D Point

    we would need to: • Define a class • With two fields called x and y
  120. @ToddGinsberg Records (Preview) If we wanted an immutable 2-D Point

    we would need to: • Define a class • With two fields called x and y • And an all-arguments constructor
  121. @ToddGinsberg Records (Preview) If we wanted an immutable 2-D Point

    we would need to: • Define a class • With two fields called x and y • And an all-arguments constructor • And getters
  122. @ToddGinsberg Records (Preview) If we wanted an immutable 2-D Point

    we would need to: • Define a class • With two fields called x and y • And an all-arguments constructor • And getters • And equals()
  123. @ToddGinsberg Records (Preview) If we wanted an immutable 2-D Point

    we would need to: • Define a class • With two fields called x and y • And an all-arguments constructor • And getters • And equals() • And hashcode()
  124. @ToddGinsberg Records (Preview) If we wanted an immutable 2-D Point

    we would need to: • Define a class • With two fields called x and y • And an all-arguments constructor • And getters • And equals() • And hashcode() • And toString()
  125. @ToddGinsberg Records (Preview) This pattern is VERY common!

  126. @ToddGinsberg Records (Preview) This pattern is VERY common! record Point(int

    x, int y) {}
  127. @ToddGinsberg Records (Preview) This pattern is VERY common! record Point(int

    x, int y) {} var point = new Point(1, 2);
  128. @ToddGinsberg Records (Preview) This pattern is VERY common! record Point(int

    x, int y) {} var point = new Point(1, 2); System.out.println("X: " + point.x()); System.out.println("Y: " + point.y());
  129. @ToddGinsberg Records (Preview) This pattern is VERY common! record Point(int

    x, int y) {} var point = new Point(1, 2); System.out.println("X: " + point.x()); System.out.println("Y: " + point.y());
  130. @ToddGinsberg Records (Preview) record Point(int x, int y) { public

    Point { if(x < 0) throw new IllegalArgumentException(); if(y < 0) throw new IllegalArgumentException(); } }
  131. @ToddGinsberg Records (Preview) record Point(int x, int y) { public

    Point { if(x < 0) throw new IllegalArgumentException(); if(y < 0) throw new IllegalArgumentException(); } public Point(Point3D other) { this(other.x(), other.y()); } }
  132. @ToddGinsberg Records (Preview) • Records extend java.lang.Record

  133. @ToddGinsberg Records (Preview) • Records extend java.lang.Record • Records are

    final
  134. @ToddGinsberg Records (Preview) • Records extend java.lang.Record • Records are

    final • Record fields are final
  135. @ToddGinsberg Records (Preview) • Records extend java.lang.Record • Records are

    final • Record fields are final • Cannot define any more fields
  136. @ToddGinsberg Syntax - Sealed Classes (Preview) sealed interface Message permits

    Start, Stop, Timeout {}
  137. @ToddGinsberg Syntax - Sealed Classes (Preview) sealed interface Message permits

    Start, Stop, Timeout {}
  138. @ToddGinsberg Syntax - Sealed Classes (Preview) sealed interface Message permits

    Start, Stop, Timeout {} record Start(String serverName) implements Message {} record Stop(String reason) implements Message {} record Timeout(Duration fromNow) implements Message {}
  139. @ToddGinsberg Syntax - Sealed Classes (Preview) sealed interface Message permits

    Start, Stop, Timeout {} record Start(String serverName) implements Message {} record Stop(String reason) implements Message {} record Timeout(Duration fromNow) implements Message {} // ERROR! class Restart implements Message {}
  140. @ToddGinsberg Syntax - Sealed Classes (Preview) sealed interface Message permits

    Start, Stop, Timeout {} record Start(String serverName) implements Message {} record Stop(String reason) implements Message {} record Timeout(Duration fromNow) implements Message {} // ERROR! class Restart implements Message {}
  141. @ToddGinsberg Syntax - Sealed Classes (Preview) // Coming in the

    future! String logMessage = switch(someMessage) { Start -> "Starting server: " + someMessage.serverName(); Stop -> "Stopping because: " + someMessage.reason(); Timeout -> "Timeout is now: " + someMessage.fromNow(); };
  142. In Conclusion...

  143. @ToddGinsberg Upgrading Past Java 8 • You have one more

    “Big Bang”
  144. @ToddGinsberg Upgrading Past Java 8 • You have one more

    “Big Bang” • And then it won’t be that bad
  145. @ToddGinsberg Upgrading Past Java 8 • You have one more

    “Big Bang” • And then it won’t be that bad • Because you’ll have new toys to play with
  146. @ToddGinsberg Upgrading Past Java 8 • You have one more

    “Big Bang” • And then it won’t be that bad • Because you’ll have new toys to play with • And performance improvements every six months!
  147. @ToddGinsberg todd@ginsberg.com https://todd.ginsberg.com Thank You! Anonymous Feedback!

  148. Todd Ginsberg Principal Developer Netspend @toddginsberg Java Cram Session https://jconf.dev