Java 39: Text Blocks, Switch Expressions & Java 8→17 Recap
easy⏱ 5 mincoursejava
Text blocks (Java 15)
"""multi-line string""" — preserves indentation relative to the least-indented line, stripping common leading whitespace. Backslash escapes work. Great for JSON, SQL, and HTML literals inline.
String json = """
{
"name": "%s",
"age": %d
}
""".formatted(name, age);
Switch expressions (Java 14)
Arrow syntax with no fall-through, returns a value, must be exhaustive over enums/sealed types. yield for multi-statement branches. Replaces 90% of switch statements.
int dayLetters = switch (day) {
case MON, WED, FRI -> 1;
case TUE, THU -> 2;
case SAT, SUN -> {
System.out.println("weekend");
yield 3;
}
};
The full modern Java map
8: lambdas, streams, Optional, default methods, java.time. 9: modules, var in lambda params, List.of/Map.of. 10: var. 11: HttpClient. 14: switch expressions. 15: text blocks. 16: records, instanceof pattern. 17: sealed classes (LTS). 21: virtual threads, switch patterns, record patterns (LTS).
Build a feature-version matrix
Create JavaFeature[] with { name, since, example, replaces }. Dump a table sorted by version. Add a helper supports(version, feature) that returns true if the version >= feature's since. Verify Java 11 supports var but not records.
Target the LTS you'll ship
LTS releases (8, 11, 17, 21) get long-term bug fixes. Target Java 17 or 21 for new services — Java 8 is end-of-support (extended only via paid plans). --release 17 ensures the compiler refuses to emit code that won't run on 17, even with a newer JDK.
Quiz: LTS versions
Which Java versions are LTS? 8, 11, 17, 21 — with 25 coming up. Non-LTS versions get 6 months of support. Oracle charges for extended LTS support; OpenJDK distros (Amazon Corretto, Eclipse Temurin) give it for free.