Previous posts:

Java 16 (2021)

Exhaustive feature list. See also Java 16: New Features

Java 17 (2021 - LTS)

Java 17 itself brings only changes related to specialized use cases (like floating-point semantics or pseudo-random numbers), but no news for most developers. But since most applications will upgrade straight from the last LTS, here are some important enhancements since Java 11, from a developer’s perspective (excluding incubations and previews):

Switch expressions

  • use the arrow syntax to avoid fall-through (without “break” statements), use colons for multiple values:
  switch (LocalDate.now().getDayOfWeek()) {
    case FRIDAY -> System.out.println("Today is almost weekend");
    case SATURDAY, SUNDAY -> {
      System.out.println("hurray!");
      System.out.println("Today is weekend");
    }
    default -> System.out.println("Today is working day");
  }
  
  • switch statements can be used as expressions (using the “yield” statement if blocks are necessary):
  var dayType = switch (LocalDate.now().getDayOfWeek()) {
    case FRIDAY -> "almost weekend";
    case SATURDAY, SUNDAY -> {
      System.out.println("hurray!");
      yield "weekend";
    }
    default -> "working day";
  };
  System.out.println("Today is " + dayType);
  
  • “yield” can also be used in traditional switch syntax:
  var dayType = switch (LocalDate.now().getDayOfWeek()) {
    case FRIDAY:
      yield "almost weekend";
    case SATURDAY, SUNDAY:
      System.out.println("hurray!");
      yield "weekend";
    default:
      yield "working day";
  };
  System.out.println("Today is " + dayType);
  
  • No “default” block is required if enum values are exhaustive:
  var dayType = switch (LocalDate.now().getDayOfWeek()) {
    case FRIDAY -> "almost weekend";
    case SATURDAY, SUNDAY -> "weekend";
    case MONDAY, TUESDAY, WEDNESDAY, THURSDAY -> "working day";
  };
  System.out.println("Today is " + dayType);
  

Text blocks

  • A text block starts with a tripple doublequote, followed by a newline
  • the line with the smallest indentation determines how many leading chars are ignored for each line (“tab” counts as one char)
System.out.println("""
   First line, 3 blanks = 1 char indentation
  Second line, 2 blanks = no indentation

			Forth line, 3 tabs = 1 tab indentation""");

Pattern Matching for instanceof

  • declare a variable as part of the “instanceof” operator to avoid a cast
Object value = /* some arbitrary object */
if (value instanceof Number x) {
  System.out.println(x.doubleValue());
} else if (value instanceof String x) {
  System.out.println(x.toUpperCase());
} else {
  System.out.println(value);
}

Records

  • Records are immutable objects (aka “value objects”) that provide a constructor, getters, an “equals()” implementation (that compares all fields) as well as “hashCode()” and “toString()” implementations
  • The body of a record may define behaviour (i.e. instance methods) but must not define state (i.e. no instance variables)
record Person(String firstName, String lastName) {
   //String address; // no instance fields allowed
   public String fullName() {
     return firstName + " " + lastName;
   }
}

var p1 = new Person("John", "Doe");
var p2 = new Person("John", "Doe");
assert p1 != p2;
assert p1.equals(p2);
assert p1.toString().equals("Person[firstName=John, lastName=Doe]");

Packaging tool

Creates an installation package (or a ready-to-use executable) for a jar file on the current operating system.

Example usage (for both installation package and executable):

jpackage --name java17app --input target --main-jar sample.jar --main-class my.package.MyMainClass
jpackage --name java17app --input target --main-jar sample.jar --main-class my.package.MyMainClass --type app-image

To create an installation package, additional software may be required on some platforms (e.g. WiX on Windows).

comments powered by Disqus