Workbench / Volume 3 Companion: The Backend
V3·39
Modern Java
Companion to Volume 3, chapter 39: how the release train, previews, language features, new APIs, modules and runtime changes connect; new angles (a release train as a timetable, a preview as a dish on the specials board, --release as cooking to an older menu); what the chapter simplifies, measured with JDK 21 and JDK 25 on the build's stage 40: a build that broke on JDK 25 because a newer compiler had a new warning, the same source giving different class files from two compilers, a 0.2.0 release that said it was 0.1.0, and upgrade gains that look large until you measure live data; the theory behind compatibility; how javac finds a dangling doc comment; four debugging drills; interview questions with how to approach each.
Understanding it better
Concept map
Chapter 39 has five parts: how Java evolves (the release train, JEPs and previews, other languages' processes), language features (from var to flexible constructor bodies, with pattern matching at the centre), libraries and APIs (sequenced collections, other additions, FFM, concurrency recap), the module system and runtime evolution (JVM and GC changes, deprecations and removals, tracking change).
The links worth remembering:
- Compatibility has layers. Source (does it compile?), binary (do old class files link?), behavioural (does it do the same thing?) and tooling (do the build's flags still pass?).
--releaseprotects the first for APIs; nothing protects the last but running the build on the new JDK. - Records, sealed types and pattern matching are one feature in three parts. Data with a fixed shape, a closed set of shapes, and a switch the compiler checks against both.
- The runtime improves under unchanged code. Most of an upgrade's benefit (GC, startup, memory) arrives without a source change; most of its cost is in tooling, flags and dependencies.
Explain it differently
1. The release train is a timetable. A train leaves every six months whether or not your feature is aboard; every fourth one (two years) is an express that stops at every station for years (LTS). Missing a train costs six months, not a whole release.
2. A preview feature is a dish on the specials board. It's fully cooked and you can order it (with --enable-preview), but it may change next week or vanish: string templates did. Don't put it on the printed menu (a library, or code you can't change every six months).
3. --release 21 is cooking to an older menu in a newer kitchen. The newer kitchen (javac 25) checks you only use ingredients the old menu lists (JDK 21's API). But the kitchen's own rules also changed: its inspector (the lint) now flags things the old one didn't (Gaps 1), and it lays out the plates a little differently (Gaps 2).
4. Sealed types are a guest list. The bouncer (the compiler) knows every name, so a switch over guests can be checked for completeness; add a name and every switch must say what to do with them.
5. Deprecation for removal is an eviction notice. @Deprecated(forRemoval = true) gives a date in a future release; jdeprscan reads every notice in your jars.
6. A service's version is a label printed at packing time. If someone prints it by hand on the box (a constant in the code), it will eventually be wrong (Gaps 3).
Gaps and misconceptions
What the chapter oversimplifies
All outputs are real, from OpenJDK 21.0.12 and Temurin 25.0.1 on the build's stage 40 (exp/40), unless stated.
1. --release 21 doesn't make a newer compiler accept your build. The chapter and its exercises recommend compiling with --release and -Xlint:all, and upgrading by running the build on the new JDK. TiffinBox compiles with --release 21 -Werror -Xlint:all. On JDK 25:
JDK lint core server tbx golden notes
21.0.12.1 ok ok ok ok ok
25.0.1 ok FAIL FAIL FAIL - core: src/in/tiffinbox/core/Money.java:22: warning: [dangling-doc-comments] documentation commen; server: src/in/tiffinbox/server/Server.java:8: error: cannot find symbol; tbx: src/in/tiffinbox/tbx/Main.java:3: error: package in.tiffinbox.core does not exist;
-Xlint:all means "all the warnings this compiler knows", and javac 23 added dangling-doc-comments. --release fixes the API you compile against, not the compiler's checks. Both warnings were real: a /** Subtracts, but never below zero… */ left above split() when minusFloorZero was written, so minusFloorZero had no documentation and javadoc silently used only the second comment for split. Treat a JDK upgrade as a change to the build's rules, and decide per release whether new lint categories should fail it.
2. Same source, same --release, different compiler: different class files. Compiling core with javac 21 and javac 25, both --release 21:
./in/tiffinbox/rules/Analyzer.class
./in/tiffinbox/rules/Interpreter.class
./in/tiffinbox/rules/Facts.class
./in/tiffinbox/core/Extensions.class
./in/tiffinbox/core/Transition.class
./in/tiffinbox/core/KitchenQueue.class
./in/tiffinbox/core/OrderCodec.class
7 of 80 class files differ: same size, same bytecode in javap -c, but the constant pool in a different order (the lambda and record bootstrap entries come earlier in javac 25's output). And jar writes Created-By: 21.0.12.1 (Ubuntu) into the manifest. Builds are reproducible only with the compiler pinned too: the JDK version belongs in the build's inputs, like a lockfile (stage 30 did this for the web build).
3. Nothing in Java tells you which version of your software is running. The chapter covers tracking JDK versions. TiffinBox's own version was a constant, VERSION = "0.1.0", and the release check printed it without checking it. Release 0.2.0's own log:
tiffinbox 0.1.0
tiffinbox 0.1.0 on port 18181: recovered 0 records, 0 orders, torn tail 0 bytes; 4 rules
Stage 40 writes the version into the jar's manifest from one file, reads it with Package.getImplementationVersion(), adds the JDK (Runtime.version()), and makes the release check fail on a mismatch:
tiffinbox 0.3.0-dev (Java 21.0.12.1+1-1-24.04.4-Ubuntu)
When the same jar runs on two JDKs (Gaps 4), "which JDK answered this request?" is the first question in any incident.
4. Upgrade gains are smaller, and easier to mismeasure, than release notes suggest. The lab measures compact headers and AOT startup on purpose-built programs. The same TiffinBox jar (compiled --release 21) after 2,000 simulated orders, measured first without a GC:
JDK 21 first /health in 335 ms; after 2,000 orders RSS 113 MB, heap used 52210K
JDK 25 first /health in 259 ms; after 2,000 orders RSS 108 MB, heap used 32262K
JDK 25, compact object headers first /health in 241 ms; after 2,000 orders RSS 110 MB, heap used 26083K
That looks like 38% less heap. After a full GC (live data only):
JDK 21 first /health in 316 ms; after 2,000 orders RSS 115 MB, heap after a full GC: used 4302K
JDK 25 first /health in 355 ms; after 2,000 orders RSS 108 MB, heap after a full GC: used 4234K
JDK 25, compact object headers first /health in 258 ms; after 2,000 orders RSS 109 MB, heap after a full GC: used 3822K
Live data differs by 2% between JDK 21 and 25, and compact headers save 11% of it. "Heap used" before a GC measures when the collector last ran, not what the program holds. Startup times moved between 258 and 355 ms across runs of the same JDK, so a 20% "improvement" in one run is noise. Measure upgrades on the real workload, repeatedly, and on live data.
Common misconceptions
| Misconception | What's actually true | Where it's shown |
|---|---|---|
"--release 21 makes any newer JDK build my code the same way." |
Same API; different lints and different class files. | Gaps 1, 2 |
"-Xlint:all -Werror is a fixed standard." |
It grows with each compiler. | Gaps 1 |
| "A version constant in the code is the version." | Only if someone remembers to change it. | Gaps 3; stage 40 |
| "Heap used shows memory savings." | Only after a full GC; otherwise it shows GC timing. | Gaps 4 |
| "Adding a method to an interface is always compatible." | Sources can clash (getFirst). |
Exercises P3 |
| "Preview features are safe once they compile." | They can change or be withdrawn next release. | the chapter, "Preview features" |
| "Upgrading the JDK means rewriting code." | Most gains come with no code change. | Gaps 4; the chapter's lab |
Theoretical gaps: the ideas behind the chapter
1. Kinds of compatibility. The JLS (chapter 13, "Binary Compatibility") defines which changes keep old class files linking: adding methods is binary-compatible, removing or changing signatures isn't. Source compatibility (does old source still compile?) and behavioural compatibility are separate; the JDK's CSR process reviews each change against all three.
2. Algebraic data types. Records are product types (a value has all fields); sealed hierarchies are sum types (a value is one of the cases). Pattern matching is case analysis over them, and exhaustiveness checking is the compiler proving every case is handled, as in ML (1970s) and Haskell.
3. Semantic versioning. MAJOR.MINOR.PATCH, where MAJOR changes break compatibility. The JDK uses time-based versions instead (one number per release, JEP 322), with compatibility promised by policy rather than by number.
4. Reproducible builds. Identical inputs give bit-identical outputs (reproducible-builds.org). Inputs include the toolchain version, timestamps, file order and locale; Gaps 2 shows the toolchain is one.
5. Modules and encapsulation. JPMS (JSR 376) adds readability (who may depend on whom) and accessibility (what's exported) checked at compile and run time; strong encapsulation of JDK internals (JEP 403, JDK 17) is why old reflection hacks stopped working.
Applying it
Code walkthrough
Gaps 1 comes from how javac 23 and later notice doc comments that belong to no declaration. From openjdk/jdk25u, src/jdk.compiler/share/classes/com/sun/tools/javac/parser/JavacParser.java.
The design, in the source's own words
/** Record nearby documentation comments against the
* primary documentation comment for a declaration.
*
* Dangling documentation comments are handled as follows.
* 1. {@code Scanner} adds all doc comments to a queue of
* recent doc comments. The queue is flushed whenever
* it is known that the recent doc comments should be
* ignored and should not cause any warnings.
* 2. The primary documentation comment is the one obtained
* from the first token of any declaration.
* (using {@code token.getDocComment()}.
The scanner keeps every /** … */ it sees in a queue. When the parser reaches a declaration, the comment attached to its first token is the primary one: the last doc comment before it.
Saving the others
private void saveDanglingDocComments(Comment dc) {
var recentComments = S.getDocComments();
switch (recentComments.size()) {
case 0:
// no recent comments
return;
case 1:
if (recentComments.peek() == dc) {
// no other recent comments
recentComments.remove();
return;
}
}
var lb = new ListBuffer<Comment>();
while (!recentComments.isEmpty()) {
var c = recentComments.remove();
if (c != dc) {
lb.add(c);
}
}
danglingComments.put(dc, lb.toList());
}
Any other queued doc comment is dangling, filed under the primary one. In Money.java, the primary comment for split was "Splits into n parts…", and "Subtracts, but never below zero…" was filed as dangling.
Reporting later, when suppression is known
void reportDanglingDocComment(Comment c) {
var pos = c.getPos();
if (pos != null) {
deferredLintHandler.report(lint -> {
if (lint.isEnabled(Lint.LintCategory.DANGLING_DOC_COMMENTS) &&
!shebang(c, pos)) {
log.warning(
pos, LintWarnings.DanglingDocComment);
}
});
}
}
The parser doesn't yet know whether the declaration has @SuppressWarnings("dangling-doc-comments"), so it defers the report to attribution, when lint settings for that declaration are known. shebang exempts a /// comment on the very first line (a script's #!-like header for JDK 25's compact source files).
Debugging drills
D1. The upgrade branch fails on warnings nobody changed
A team bumps CI from JDK 21 to JDK 25 without touching code. The build fails with warning: [dangling-doc-comments] and warnings found and -Werror specified, in files untouched for years.
D2. Two identical builds, two different jar checksums
The release pipeline checks that the jar built by CI has the same SHA-256 as one built on a developer's machine from the same commit. It fails, although both used --release 21 and the same Gradle version.
D3. "Which version is in production?"
During an incident, the dashboard says production runs 2.4.0 because /version says so, but the bug was fixed in 2.4.0 and is still happening.
D4. "JDK 25 halved our memory"
After an upgrade, a team reports "heap used" dropping from 900 MB to 450 MB and plans to halve container memory.
Career-oriented
Interview prep
Mid-level 1: records against classes
Question. "When would you use a record, and when a regular class?"
How to get there. What a record promises, then what it gives up.
Mid-level 2: a pattern-matching switch
Question. "Rewrite an if (e instanceof A) … else if (e instanceof B) … else return "unknown" chain over events with modern Java, and say what you gain."
How to get there. Seal the hierarchy, switch over it, drop the default.
Senior 1: planning a JDK upgrade for one service
Question. "Plan the upgrade of a Spring service from JDK 17 to 25."
How to get there. Dependencies, build, runtime, rollout.
Senior 2: using preview features
Question. "A team wants structured concurrency (preview in JDK 25) in production. What do you say?"
How to get there. What preview means, cost of change, containment.
Staff 1: an upgrade policy for many services
Question. "We have 60 services on JDK 11, 17 and 21. Propose a JDK policy."
How to get there. Targets, cadence, enforcement, exceptions.
Staff 2: proving an upgrade's value
Question. "Leadership asks what moving to JDK 25 is worth. How do you answer with numbers?"
How to get there. Measure the right quantities, on real workloads, with variance.