Previous posts:
Java 16 (2021)
- The “instanceof pattern matching” feature (from Java 14) is now final
- Records (also a Java 14 preview) are now final
- a new incubating Vector API
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 statements can be used as expressions (using the “yield” statement if blocks are necessary):
- “yield” can also be used in traditional switch syntax:
- No “default” block is required if enum values are exhaustive:
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)
Pattern Matching for instanceof
- declare a variable as part of the “instanceof” operator to avoid a cast
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)
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