Skip to content

Java 13

Neelu edited this page Jul 26, 2026 · 1 revision

☕ Java 13: Text Blocks Preview

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


📋 Table of Contents

  1. Overview
  2. Core Features
  3. Best Practices

🎯 Overview

Java 13 refines two previews from the previous release: switch expressions gain the yield keyword, and a brand-new preview arrives — text blocks, ending decades of string-concatenation pain for multi-line text (SQL, JSON, HTML).

flowchart LR
    A["String s = 'line1\\n'\n+ 'line2\\n'\n+ 'line3';"] -->|JEP 355 Preview| B["String s = '''\n    line1\n    line2\n    line3\n    ''';"]
Loading

🚀 Core Features

1️⃣ Text Blocks (Preview)

What: Multi-line string literals delimited by """, with smart, automatic indentation stripping.

// Before: escape hell for a JSON payload
String json = "{\n" +
              "  \"name\": \"Neelu\",\n" +
              "  \"role\": \"Java Architect\"\n" +
              "}";

// Java 13 text block
String json = """
    {
      "name": "Neelu",
      "role": "Java Architect"
    }
    """;

// SQL example — no more string concatenation for multi-line queries
String query = """
    SELECT id, name, salary
    FROM employees
    WHERE department = ?
    ORDER BY salary DESC
    """;

Indentation rules — the compiler strips the common minimum leading whitespace based on the closing """ position:

String html = """
              <html>
                  <body>
                      <p>Hello</p>
                  </body>
              </html>
              """;
// Indentation relative to the closing delimiter is preserved,
// common leading whitespace across all lines is stripped
flowchart TD
    A["Raw text block source"] --> B{"Find minimum\nindentation across\nall non-blank lines"}
    B --> C["Strip that amount\nfrom every line"]
    C --> D["Trailing whitespace\nremoved per line"]
    D --> E["Final String value"]
Loading

2️⃣ Switch Expressions — yield Keyword (2nd Preview)

int result = switch (day) {
    case MONDAY, FRIDAY, SUNDAY -> 6;
    case TUESDAY -> 7;
    case THURSDAY, SATURDAY -> 8;
    default -> {
        int len = day.toString().length();
        yield len;          // yield replaces break-with-value from the 1st preview
    }
};

yield was introduced specifically because break value; (used in the Java 12 preview) was ambiguous and confusing next to labeled break.


3️⃣ Dynamic CDS Archives

What: Extends AppCDS (Java 10) so you no longer need a separate training run to build the class list — the archive can be created automatically at JVM exit.

# Single command — archive created automatically when the JVM exits normally
java -XX:ArchiveClassesAtExit=app-dynamic.jsa -cp app.jar Main

# Subsequent runs use it for faster startup
java -XX:SharedArchiveFile=app-dynamic.jsa -cp app.jar Main

4️⃣ ZGC: Uncommit Unused Memory

ZGC (introduced experimentally in Java 11) can now return unused heap memory to the OS instead of holding onto it — important for containerized/cloud deployments where memory is billed.

java -XX:+UnlockExperimentalVMOptions -XX:+UseZGC -Xmx4g -jar app.jar

💡 Best Practices

✅ Reach for text blocks anywhere you have SQL, JSON, HTML, or multi-line log messages hardcoded in Java
✅ Use yield (not break value) once on Java 13+ previews — it's the syntax that stuck
❌ Don't rely on exact preview syntax in production code — text blocks changed slightly before standardizing in Java 15

// Text blocks + String.formatted() (Java 15+) is a killer combo for readable templates
String message = """
    Hello, %s!
    You have %d new notifications.
    """.formatted(userName, count);

Clone this wiki locally