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 full-size slide

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

    View full-size slide

  3. @ToddGinsberg
    1

    View full-size slide

  4. @ToddGinsberg
    @ToddGinsberg
    Syntax - instanceof Pattern Matching

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  7. @ToddGinsberg
    2

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size slide

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

    View full-size slide

  13. @ToddGinsberg
    3

    View full-size slide

  14. @ToddGinsberg
    @ToddGinsberg
    Records

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size 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 full-size 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 full-size 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 full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  35. @ToddGinsberg
    @ToddGinsberg
    Records

    View full-size slide

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

    View full-size slide

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

    View full-size 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 full-size slide

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

    View full-size slide