Modern Java

Sprachfeatures von JDK 11 bis heute in der Praxis

πŸ‘¨πŸ»β€πŸ’» 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