Upgrade to Pro — share decks privately, control downloads, hide ads and more …

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

Todd Ginsberg
September 30, 2020

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.

Todd Ginsberg

September 30, 2020
Tweet

More Decks by Todd Ginsberg

Other Decks in Technology

Transcript

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

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

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

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

    deprecations • New libraries and methods that you may not need right away
  5. @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
  6. @ToddGinsberg Current State Java 14 Java 15 Java 16 No

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

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

    only • Miss the train? Wait for the next one • Allows for preview features
  9. @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
  10. @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
  11. @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
  12. @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
  13. @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
  14. @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...
  15. @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
  16. @ToddGinsberg JPMS (Modules): Recommendation • Don’t worry about it for

    now • Take advantage of the fact that it made the JVM more organized
  17. @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
  18. @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!
  19. @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!
  20. @ToddGinsberg Where Did JavaFX Go? • Removed from the JVM!

    • Still maintained, don’t panic! https://openjfx.io/
  21. @ToddGinsberg New! Compact Strings • Java char takes up two

    bytes • All Strings are char x2 bytes in Java 8
  22. @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
  23. @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
  24. @ToddGinsberg New Garbage Collector Options • G1GC is the new

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

    default • CMS is now deprecated • Epsilon (for testing) • ZGC is experimental
  26. @ToddGinsberg Syntax - Local var public void doSomething() { String

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

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

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

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

    Yes var connection = getConnection(); var now = LocalTime.now(); }
  31. @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);
  32. @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);
  33. @ToddGinsberg Library - String Enhancements String sample = "1\n2\n3"; sample

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

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

    "; sample.strip() // "sample" sample.stripLeading() // "sample " sample.stripTrailing() // " sample"
  36. @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)
  37. @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)
  38. @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)
  39. @ToddGinsberg Syntax - Text Blocks String json = "{\n" +

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

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

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

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

    { case 1: { // Some other work here yield "French Fries"; } case 2: yield "Doughnuts"; default: yield "Pizza"; };
  44. @ToddGinsberg Syntax - instanceof Pattern Matching (Preview) if(someObject instanceof String)

    { String someString = (String)someObject; doSomethingWithString(someString); }
  45. @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
  46. @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
  47. @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
  48. @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()
  49. @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()
  50. @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()
  51. @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());
  52. @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());
  53. @ToddGinsberg Records (Preview) record Point(int x, int y) { public

    Point { if(x < 0) throw new IllegalArgumentException(); if(y < 0) throw new IllegalArgumentException(); } }
  54. @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()); } }
  55. @ToddGinsberg Records (Preview) • Records extend java.lang.Record • Records are

    final • Record fields are final • Cannot define any more fields
  56. @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 {}
  57. @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 {}
  58. @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 {}
  59. @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(); };
  60. @ToddGinsberg Upgrading Past Java 8 • You have one more

    “Big Bang” • And then it won’t be that bad
  61. @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
  62. @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!