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
  2. @ToddGinsberg Three New Features Coming to Java Chicago JUG Todd

    Ginsberg Principal Software Developer 2020-12-10 2 ½
  3. @ToddGinsberg @ToddGinsberg Syntax - instanceof Pattern Matching if(someObject instanceof String)

    { String someString = (String)someObject; doSomethingWithString(someString); }
  4. @ToddGinsberg @ToddGinsberg Sealed Types (Preview) Have you ever wanted… •

    To limit the implementations of an interface? • Or subclases to your class?
  5. @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?
  6. @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
  7. @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
  8. @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
  9. @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()
  10. @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()
  11. @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()
  12. @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());
  13. @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());
  14. @ToddGinsberg @ToddGinsberg Records record Point(int x, int y) { public

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

    final • Record fields are final • Cannot define any more fields
  17. @ToddGinsberg @ToddGinsberg Records • Think of every DTO you’ve ever

    written • Or every Pair<A,B> class you’ve written
  18. @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