Fundamentals 10 min read

Java 14 New Features: Pattern Matching, Records, Switch Expressions & Text Blocks

Java 14 introduces several language enhancements—including preview pattern‑matching for instanceof, concise record types, switch expressions with arrow syntax, and multi‑line text blocks—each demonstrated with code examples, setup steps, and compiled bytecode insights to help developers quickly adopt these productivity‑boosting features.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Java 14 New Features: Pattern Matching, Records, Switch Expressions & Text Blocks

Java 14 has been released and brings a set of new language features that improve developer productivity. This article walks through the most interesting additions, showing how to set up the environment and providing concise code examples.

01. Download JDK 14

Download the open‑source JDK from jdk.java.net (or the commercial version from Oracle) and unzip it on Windows.

02. Upgrade IntelliJ IDEA

Install the IntelliJ IDEA 2020.1 EAP (Early Access Program) to get support for Java 14 features. The download URL is:

https://www.jetbrains.com/idea/nextversion/

01. Pattern‑matching instanceof

Traditional instanceof requires a separate cast:

public class OldInstanceOf {
    public static void main(String[] args) {
        Object str = "Java 14,真香";
        if (str instanceof String) {
            String s = (String) str;
            System.out.println(s.length());
        }
    }
}

Java 14 preview adds pattern‑matching, allowing the variable to be declared inline:

public class NewInstanceOf {
    public static void main(String[] args) {
        Object str = "Java 14,真香";
        if (str instanceof String s) {
            System.out.println(s.length());
        }
    }
}

Enable the preview feature in the project language level to compile without errors. The compiled bytecode shows the implicit cast and variable assignment.

public class NewInstanceOf {
    public static void main(String[] args) {
        Object str = "Java 14,真香";
        String s;
        if (str instanceof String && (s = (String) str) == (String) str) {
            System.out.println(s.length());
        }
    }
}

02. Records

Records provide a compact syntax for immutable data carriers. A record automatically generates equals(), hashCode(), toString(), a constructor, and accessor methods.

public record Writer(String name, int age) { }

Using the record:

public class WriterDemo {
    public static void main(String[] args) {
        Writer writer = new Writer("沉默王二", 18);
        System.out.println("toString:" + writer);
        System.out.println("hashCode:" + writer.hashCode());
        System.out.println("name:" + writer.name());
        System.out.println("age:" + writer.age());

        Writer writer1 = new Writer("沉默王二", 18);
        System.out.println("equals:" + writer.equals(writer1));
    }
}

Output:

toString:Writer[name=沉默王二, age=18]
hashCode:1130697218
name:沉默王二
age:18
equals:true

03. Switch Expressions

Switch expressions are now a standard feature, allowing the arrow syntax and returning a value directly.

private static String createPlayer(PlayerTypes playerType) {
    return switch (playerType) {
        case TENNIS -> "网球运动员费德勒";
        case FOOTBALL -> "足球运动员C罗";
        case BASKETBALL -> "篮球运动员詹姆斯";
        case PINGPANG -> "乒乓球运动员马龙";
        case UNKNOWN -> throw new IllegalArgumentException("未知");
    };
}

04. Text Blocks

Before Java 14, multi‑line strings required concatenation and escape sequences. Text blocks simplify this:

// Old way
String html = "<html>
" +
              "    <body>
" +
              "        <p>Hello, world</p>
" +
              "    </body>
" +
              "</html>
";

With text blocks:

String html = """
    <html>
        <body>
            <p>Hello, world</p>
        </body>
    </html>
""";

The result is a clean, readable multi‑line string without extra quotes or plus signs.

These four enhancements—pattern‑matching instanceof, records, switch expressions, and text blocks—make Java 14 a more expressive and concise language for modern development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javapattern-matchingrecordsText BlocksSwitch ExpressionsJava 14
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.