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

Three New Features Coming to Java

Three New Features Coming to Java

This is a lightning talk I did for the Chicago Java User Group. Originally it was supposed to be three features, but another speaker was doing a lightning talk on Sealed Types, so I cut most of that. In this talk we mostly discuss instanceof and Java Records

2777430c1f9869e411ab5d8b17d8ba2a?s=128

Todd Ginsberg

December 10, 2020
Tweet

Transcript

  1. @ToddGinsberg Three New Features Coming to Java Chicago JUG Todd

    Ginsberg Principal Software Developer 2020-12-10
  2. @ToddGinsberg Three New Features Coming to Java Chicago JUG Todd

    Ginsberg Principal Software Developer 2020-12-10 2 ½
  3. @ToddGinsberg 1

  4. @ToddGinsberg @ToddGinsberg Syntax - instanceof Pattern Matching

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

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

    someString) { doSomethingWithString(someString); }
  7. @ToddGinsberg 2

  8. @ToddGinsberg @ToddGinsberg Sealed Types (Preview) Have you ever wanted…

  9. @ToddGinsberg @ToddGinsberg Sealed Types (Preview) Have you ever wanted… •

    To limit the implementations of an interface?
  10. @ToddGinsberg @ToddGinsberg Sealed Types (Preview) Have you ever wanted… •

    To limit the implementations of an interface? • Or subclases to your class?
  11. @ToddGinsberg @ToddGinsberg Sealed Types (Preview) Have you ever wanted… •

    To limit the implementations of an interface? • Or subclases to your class? • Without having to do wacky things with enums?
  12. @ToddGinsberg @ToddGinsberg Sealed Types (Preview) Neha Sardana Will cover this

    in detail!
  13. @ToddGinsberg 3

  14. @ToddGinsberg @ToddGinsberg Records

  15. @ToddGinsberg @ToddGinsberg Records Records are immutable data holders

  16. @ToddGinsberg @ToddGinsberg Records If we wanted an immutable 2-D Point

    we would need to:
  17. @ToddGinsberg @ToddGinsberg Records If we wanted an immutable 2-D Point

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

    we would need to: • Define a class • With two fields called x and y
  19. @ToddGinsberg @ToddGinsberg Records 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
  20. @ToddGinsberg @ToddGinsberg Records 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
  21. @ToddGinsberg @ToddGinsberg Records 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()
  22. @ToddGinsberg @ToddGinsberg Records 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()
  23. @ToddGinsberg @ToddGinsberg Records 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()
  24. @ToddGinsberg @ToddGinsberg Records This pattern is VERY common!

  25. @ToddGinsberg @ToddGinsberg Records This pattern is VERY common! record Point(int

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

    x, int y) {} var point = new Point(1, 2);
  27. @ToddGinsberg @ToddGinsberg Records 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());
  28. @ToddGinsberg @ToddGinsberg Records 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());
  29. @ToddGinsberg @ToddGinsberg Records record Point(int x, int y) { public

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

  32. @ToddGinsberg @ToddGinsberg Records • Records extend java.lang.Record • Records are

    final
  33. @ToddGinsberg @ToddGinsberg Records • Records extend java.lang.Record • Records are

    final • Record fields are final
  34. @ToddGinsberg @ToddGinsberg Records • Records extend java.lang.Record • Records are

    final • Record fields are final • Cannot define any more fields
  35. @ToddGinsberg @ToddGinsberg Records

  36. @ToddGinsberg @ToddGinsberg Records • Think of every DTO you’ve ever

    written
  37. @ToddGinsberg @ToddGinsberg Records • Think of every DTO you’ve ever

    written • Or every Pair<A,B> class you’ve written
  38. @ToddGinsberg @ToddGinsberg Records • Think of every DTO you’ve ever

    written • Or every Pair<A,B> class you’ve written • Or every time you wished you had a better Map key
  39. @ToddGinsberg @ToddGinsberg todd@ginsberg.com https://todd.ginsberg.com Thank You! Anonymous Feedback!