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

Todd Ginsberg

December 10, 2020
Tweet

More Decks by Todd Ginsberg

Other Decks in Technology

Transcript

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

    View Slide

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

    View Slide

  3. @ToddGinsberg
    1

    View Slide

  4. @ToddGinsberg
    @ToddGinsberg
    Syntax - instanceof Pattern Matching

    View Slide

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

    View Slide

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

    View Slide

  7. @ToddGinsberg
    2

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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?

    View Slide

  12. @ToddGinsberg
    @ToddGinsberg
    Sealed Types (Preview)
    Neha Sardana
    Will cover this in detail!

    View Slide

  13. @ToddGinsberg
    3

    View Slide

  14. @ToddGinsberg
    @ToddGinsberg
    Records

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  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()

    View Slide

  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()

    View Slide

  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()

    View Slide

  24. @ToddGinsberg
    @ToddGinsberg
    Records
    This pattern is VERY common!

    View Slide

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

    View Slide

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

    View Slide

  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());

    View Slide

  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());

    View Slide

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

    View Slide

  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());
    }
    }

    View Slide

  31. @ToddGinsberg
    @ToddGinsberg
    Records
    ● Records extend java.lang.Record

    View Slide

  32. @ToddGinsberg
    @ToddGinsberg
    Records
    ● Records extend java.lang.Record
    ● Records are final

    View Slide

  33. @ToddGinsberg
    @ToddGinsberg
    Records
    ● Records extend java.lang.Record
    ● Records are final
    ● Record fields are final

    View Slide

  34. @ToddGinsberg
    @ToddGinsberg
    Records
    ● Records extend java.lang.Record
    ● Records are final
    ● Record fields are final
    ● Cannot define any more fields

    View Slide

  35. @ToddGinsberg
    @ToddGinsberg
    Records

    View Slide

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

    View Slide

  37. @ToddGinsberg
    @ToddGinsberg
    Records
    ● Think of every DTO you’ve ever written
    ● Or every Pair class you’ve written

    View Slide

  38. @ToddGinsberg
    @ToddGinsberg
    Records
    ● Think of every DTO you’ve ever written
    ● Or every Pair class you’ve written
    ● Or every time you wished you had a better Map key

    View Slide

  39. @ToddGinsberg
    @ToddGinsberg
    [email protected]
    https://todd.ginsberg.com
    Thank You!
    Anonymous Feedback!

    View Slide