-
Notifications
You must be signed in to change notification settings - Fork 0
Java 23
Release Date: September 17, 2024 | Type: Standard (non-LTS) | Status:
⚠️ End of Life
Java 23 pushes pattern matching further with primitive type patterns, makes writing Javadoc dramatically less painful with Markdown comments, and switches ZGC to generational mode by default.
flowchart LR
A["Pattern matching: Java 16-21\nreference types only"] -->|JEP 455 Preview| B["Primitive types in patterns\nint, double, boolean, etc."]
What: Extends pattern matching (instanceof and switch) to primitive types — previously restricted to reference types only.
// Before Java 23: switch on primitives couldn't use pattern guards elegantly
static String classify(int value) {
if (value < 0) return "negative";
if (value == 0) return "zero";
return "positive";
}
// Java 23 Preview: primitive patterns in switch
static String classify(Object obj) {
return switch (obj) {
case Integer i when i < 0 -> "negative int";
case int i -> "primitive int: " + i; // NEW: primitive pattern
case double d when d > 100.0 -> "large double";
case double d -> "double: " + d;
default -> "unknown";
};
}
// Also works with instanceof-style narrowing conversions
Object value = 42;
if (value instanceof int i) {
System.out.println("It's an int: " + i);
}Why it matters: Closes a long-standing gap where autoboxing was the only way to pattern-match on numeric values — cleaner and more efficient.
What: Write Javadoc in Markdown instead of HTML/Javadoc tags — dramatically more readable in source form.
// Before: HTML soup in Javadoc
/**
* Calculates the total price.
* <p>
* This method applies the following rules:
* <ul>
* <li>10% discount if quantity > 10</li>
* <li>Free shipping if total > $50</li>
* </ul>
*
* @param quantity the number of items
* @param unitPrice the price per item
* @return the calculated total
*/
public double calculateTotal(int quantity, double unitPrice) { ... }
// Java 23: Markdown Javadoc — use /// instead of /** */
/// Calculates the total price.
///
/// This method applies the following rules:
/// - 10% discount if quantity > 10
/// - Free shipping if total > $50
///
/// @param quantity the number of items
/// @param unitPrice the price per item
/// @return the calculated total
public double calculateTotal(int quantity, double unitPrice) { ... }flowchart LR
A["/** HTML-tagged Javadoc */"] -->|"JEP 467"| B["/// Markdown Javadoc"]
B --> C["Readable in raw source\nAND rendered HTML"]
Continued refinement — see the Java 22 guide for the core concept. Standardized in Java 24.
What: Import an entire module's exported packages with one statement — reduces import clutter, especially useful with the simplified "instance main methods" feature for scripting-style Java.
// Before: individual imports for every package you need
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
// Java 23 Preview: import the whole module
import module java.base;
void main() {
List<String> names = List.of("Alice", "Bob");
var upper = names.stream().map(String::toUpperCase).collect(Collectors.toList());
System.out.println(upper);
}# Java 21-22: had to opt in explicitly
java -XX:+UseZGC -XX:+ZGenerational -jar app.jar
# Java 23+: generational mode is the DEFAULT when ZGC is selected
java -XX:+UseZGC -jar app.jar
# Non-generational ZGC is now deprecatedContinued refinement of statements-before-super() from Java 22 — see the Java 22 guide.
✅ Start writing new Javadoc comments with /// Markdown syntax once out of preview — significantly more pleasant to read/write than HTML-tagged Javadoc
✅ Use primitive patterns to simplify numeric classification logic that previously needed boxing or separate if chains
❌ Don't mass-migrate existing HTML Javadoc to Markdown just for its own sake — do it incrementally as you touch files