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
- openvalue.training/modern-java/
- "Best Of"
- 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.*ย tojakarta.*

โจ New Features

๐ฌ Text Blocks
๐ฌ 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
๐ 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
modern-java
- 6