Skip to content

Java 23

Neelu edited this page Jul 26, 2026 · 1 revision

☕ Java 23: Primitive Patterns & Markdown Javadoc

Release Date: September 17, 2024 | Type: Standard (non-LTS) | Status: ⚠️ End of Life


📋 Table of Contents

  1. Overview
  2. Core Features
  3. Best Practices

🎯 Overview

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."]
Loading

🚀 Core Features

1️⃣ Primitive Types in Patterns (Preview)

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.


2️⃣ Markdown Documentation Comments (JEP 467)

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 &gt; 10</li>
 *   <li>Free shipping if total &gt; $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"]
Loading

3️⃣ Stream Gatherers (2nd Preview)

Continued refinement — see the Java 22 guide for the core concept. Standardized in Java 24.


4️⃣ Module Import Declarations (Preview)

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);
}

5️⃣ Generational ZGC — Now Default (JEP 474)

# 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 deprecated

6️⃣ Flexible Constructor Bodies (2nd Preview)

Continued refinement of statements-before-super() from Java 22 — see the Java 22 guide.


💡 Best Practices

✅ 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


Clone this wiki locally