Java 13 features

Java 13 (released in September 2019) is mostly about preview features and JVM improvements rather than big “everyday API” changes. Still, it introduced some very useful upgrades—especially for writing cleaner, more readable code.


1) Switch Expressions (Preview) – Cleaner switch with yield

Java 13 continues the modern switch (introduced as preview in Java 12) and improves it with the yield keyword, allowing you to return values from switch blocks.

Why it matters

  • Less boilerplate than switch + break
  • Works nicely as an expression (returns a value)
  • Safer and more readable

Example: switch expression with yield

public class SwitchExpressionDemo {
    public static void main(String[] args) {
        String day = "SATURDAY";

        int hours = switch (day) {
            case "MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY" -> 8;
            case "SATURDAY", "SUNDAY" -> {
                System.out.println("Weekend mode!");
                yield 0; // Java 13: yield returns a value from the block
            }
            default -> throw new IllegalArgumentException("Unknown day: " + day);
        };

        System.out.println("Working hours: " + hours);
    }
}

Key takeaway: Use yield when your switch arm is a block and you want to return a value.

How to run (Preview feature):

javac --enable-preview --release 13 SwitchExpressionDemo.java
java --enable-preview SwitchExpressionDemo

2) Text Blocks (Preview) – Multi-line Strings Without Ugly Escapes

Text Blocks make writing multi-line strings (JSON, HTML, SQL, XML) much cleaner.

Why it matters

  • No more \n + \" mess
  • Easier to maintain templates and queries
  • Looks like the actual text you want to produce

Example: JSON using Text Blocks

public class TextBlockDemo {
    public static void main(String[] args) {
        String json = """
                {
                  "name": "Susil",
                  "role": "Java Developer",
                  "version": 13
                }
                """;

        System.out.println(json);
    }
}

How to run (Preview feature):

javac --enable-preview --release 13 TextBlockDemo.java
java --enable-preview TextBlockDemo

Tip for blog readers: Text Blocks are perfect for APIs where you build JSON requests, SQL queries, or HTML snippets.


3) Re-implement the Legacy Socket API (JEP 353)

Java has had old networking code (java.net.Socket) built on very old internal foundations. Java 13 re-implemented the underlying socket mechanism using a modern internal implementation.

Why it matters

  • Better maintainability of the JDK
  • Better performance and reliability in some cases
  • Still backward compatible (your code doesn’t change)

Developer note: This is mainly a JVM/JDK internal improvement—you benefit without changing code.


4) Dynamic CDS Archives (JEP 350) – Faster Startup (Especially for Apps)

CDS (Class Data Sharing) helps reduce JVM startup time by sharing class metadata. Java 13 enables dynamic archiving—creating CDS archives during application execution.

Why it matters

  • Faster JVM startup (useful for microservices, CLI tools, serverless)
  • Lower memory footprint in some deployments

✅ Mostly relevant for production tuning and Java platform engineers.


5) ZGC: Uncommit Unused Memory (JEP 351)

ZGC (Z Garbage Collector) is designed for low-latency applications. Java 13 allows ZGC to return unused heap memory back to the OS.

Why it matters

  • Memory usage becomes more efficient
  • Better behavior in containerized / cloud environments
  • Helps reduce “reserved but unused” memory

✅ This matters if you run large services and want stable, predictable memory usage.


Bonus: Remove the “Domination” of Biased Locking? (Not yet)

People often confuse Java 13 with biased-locking removal. That came later (disabled by default in newer versions and eventually removed). Java 13’s concurrency story is more about JVM evolution than new keywords.


Java 13 Features – Quick Summary Table

Switch Expressions (Preview): Cleaner switch that returns values using yield
Text Blocks (Preview): Multi-line strings using """
Socket API Reimplementation: Modern internal networking improvements
Dynamic CDS Archives: Faster startup by improved class sharing
ZGC Memory Uncommit: Returns unused heap memory to OS (for ZGC)


FAQ: Should You Use Java 13 in Production?

Java 13 is a non-LTS release. Many teams prefer LTS versions (like Java 11, 17, 21) for production stability.

However, learning Java 13 is valuable because:

  • It introduced features that became stable later (especially Text Blocks & Switch expressions).
  • It helps you understand the evolution toward modern Java.