Modern Java

Sprachfeatures von JDK 11 bis heute in der Praxis

Modern Java:

Mastering Features from JDK 11 to Present

๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป Jens Knipper

  • Software Engineer
  • OpenValue Dรผsseldorf
  • curious about new technologies
  • likes to share his knowledge

๐Ÿ“Œ About this talk

  • originally 8h workshop
  • news in the Java ecosystem
  • introduce new features
    • when, what, how, ...
    • where to use it
      • refactoring!

๐Ÿ“ฐ News in the Java Ecosystem

โ˜• OpenJDK Release Process (old)

https://www.heise.de/blog/Der-Java-Release-Train-7350614.html

  • time between releases differs ย 
    • pre defined scope of releases ย 
    • released when all ready to go live ย 
    • very long time between releases
  • always timeframe where two releases supported
    • time for migration to new version

โ˜• OpenJDK Release Process (new)

https://www.heise.de/blog/Der-Java-Release-Train-7350614.html

  • a new release every six months
  • might contain no new features, but security updates
  • long-term Support (LTS) and short-term support(STS)
    • STS, lifecycle of six months until the next release
    • LTS, longer maintenance and free security updates

โ˜• OpenJDK Release Process (new)

https://www.heise.de/blog/Der-Java-Release-Train-7350614.html

  • features might be in preview: --enable-preview
  • since Java 17, LTS release every second year
  • after OpenJDK release, distributions are created
    • e.g. Eclipse Temurin, Oracle JDK, Azul Zulu, ...
    • sources may be adjusted, e.g. bundled with JavaFX

๐Ÿ—๏ธ OpenJDK Distributions

  • more than 20 different distributions
  • might differ in:
    • support
    • updates
    • compliance
    • cost
    • performance
    • features

ย  Gerrit Grunwald: Welcome to The Jungle - A Safari Through The JVM Landscape

โ›ต J2EE, JavaEE, JakartaEE

  • specification defining certain APIs
    • e.g. Bean Validation, Jakarta Server Faces, ...
  • formerly maintained by Oracle
  • 2017 announced submit to Eclipse Foundation
  • "Java" is trademarked
  • renaming to JakartaEE
  • also namespace change from javax.*ย to jakarta.*

โœจ New Features

๐Ÿ’ฌ Text Blocks

  • JEP 378

  • succeeding Raw String Literals: JEP 326
    • intended for JDK 12, but withdrawn
  • Preview: JDK 13, 14
  • Release: JDK 15
  • two new escape sequences added in JDK 14

๐Ÿ’ฌ Text Blocks

  • strings that span several lines of source code
  • enhance the readability of strings
String query = """
              SELECT "EMP_ID", "LAST_NAME" FROM "EMPLOYEE_TB"
              WHERE "CITY" = 'INDIANAPOLIS'
              ORDER BY "EMP_ID", "LAST_NAME";
              """;

๐Ÿ’ฟ Records

  • JEP 395
  • Preview: JDK 14, 15
  • Release: JDK 16
record Point(int x, int y) { }
...
var p = new Point(1, 2);
p.x();

๐Ÿ’ฟ Records

  • carriers for immutable data
  • simple aggregation of values
  • immutable data > extendable behaviour
  • automatically implement data-driven methods
    • accessors
    • equals
    • hashCode
    • toString
  • simple data aggregates with less code

๐Ÿ‘‰ Helpful Nullpointer Exceptions

  • JEP 358
  • History:
    • Preview: -
    • Release: JDK 14
    • default activated in JDK 15: Ticket
  • Summary:
    • improve the usability of NullPointerExceptions
    • describe which variable was null

๐Ÿ”€ Switch Expressions

  • JEP 361
  • Preview: JDK 12, 13 ย 
  • Release: JDK 14
  switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> System.out.println(6);
    case TUESDAY                -> System.out.println(7);
    case THURSDAY, SATURDAY     -> System.out.println(8);
    case WEDNESDAY              -> System.out.println(9);
  }

๐Ÿ”€ Switch Expressions

  • switch as a statement or an expression
  • both forms can use
    • traditional case ... : labels (with fall through)
    • new case ... -> labels (with no fall through)
      • new statement for yielding a value from a switch expression
  • simplify everyday coding
  • prepare pattern matching in switch

๐ŸŽ Pattern Matching for Switch

  • JEP 441
  • Preview: JDK 17, 18, 19, 20 ย  ย 
  • Release: JDK 21 ย  ย 
    • co-evolved with the Record Patterns
enum Decision { YES, NO }

static void decide(Decision c) {
  switch (c) {
      case null -> System.out.println("undecided");
      case YES -> System.out.println("yes");
      case NO -> System.out.println("no");
  }
}

๐ŸŽ Pattern Matching for Switch

  • test expression against patterns, each with an action ย 
  • express complex data-oriented queries concisely and safely
  • allow patterns to appear in case labels
  • combine with conditions with when
  • checking for null now possible
  • pattern switch statements have to cover all possible input values, e.g. through default
  • backwards compatibility for "old" switch statements

๐Ÿงน Refactoring

Demo

๐Ÿ”— Resources

๐Ÿ™ Thank you!

Please ask questions.

โœ‰๏ธย jens@openvalue.de

ย  ย  ย @jensknipper.de

๐ŸŒ openvalue.eu

modern-java

By Jens Knipper