What’s New in Java 14🚀

Jahid Momin
3 min readNov 3, 2024

--

Java 14 introduced a range of new features and improvements that make coding smoother, faster, and more expressive. Released in March 2020, Java 14 offers some experimental and preview features as well as some major updates. Let’s explore to get a good grasp on what’s changed in java.

1. Switch Expressions (Standard) 🛠️

Java 14 makes switch expressions a permanent feature, first introduced as a preview in Java 12 and Java 13. Now, switch can be used as both a statement and an expression, simplifying the syntax and making it more powerful.

Before:

String result;
switch (day) {
case MONDAY:
case FRIDAY:
case SUNDAY:
result = "Weekend Vibes";
break;
case TUESDAY:
case THURSDAY:
result = "Midweek Work";
break;
case WEDNESDAY:
result = "Hump Day!";
break;
default:
result = "Just Another Day";
}

With Java 14 :

String result = switch (day) {
case MONDAY, FRIDAY, SUNDAY -> "Weekend Vibes";
case TUESDAY, THURSDAY -> "Midweek Work";
case WEDNESDAY -> "Hump Day!";
default -> "Just Another Day";
};

With this new syntax, you can assign the result directly to a variable, making the code cleaner and reducing the need for multiple lines.

2. Pattern Matching for instanceof (Preview) 🔍

Java 14 introduces pattern matching for instanceof, which simplifies code by combining type checking and casting into a single step. This is especially useful for avoiding repetitive casts.

Example:

if (obj instanceof String) {
String str = (String) obj;
System.out.println(str.toUpperCase());
}

With Java 14:

if (obj instanceof String str) {
System.out.println(str.toUpperCase());
}

By using instanceof with pattern matching, the variable str is automatically cast to String, eliminating the need for manual casting.

3. Helpful NullPointerExceptions 💡

Java 14 improves NullPointerException messages by providing more detail about what caused the error, making debugging much easier.

If you have a line like this:

person.getAddress().getCity().getZipCode();

With traditional NullPointerException errors, you wouldn’t know which part was null. In Java 14, the error message might look like:

Exception in thread "main" java.lang.NullPointerException: 
Cannot read field "city" because "address" is null

4. Records (Preview) 📄

The record keyword is a new way to create simple, immutable data-carrying classes. Records automatically generate equals, hashCode, and toString methods based on the fields.

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

Person person = new Person("Alice", 30);
System.out.println(person); // Output: Person[name=Alice, age=30]

Records make data-holding classes less cluttered by reducing boilerplate code, making them great for use cases like DTOs (Data Transfer Objects).

5. Text Blocks (Second Preview) 📜

Text blocks make working with multi-line strings easier and cleaner. Instead of dealing with escape characters or concatenations, you can use triple quotes (""") to start and end a block.

String html = """
<html>
<body>
<h1>Hello, Java 14!</h1>
</body>
</html>
""";

With text blocks, the multi-line string is more readable and easier to manage, especially for code involving HTML, SQL, or JSON.

6. JFR Event Streaming 📈

Java Flight Recorder (JFR) now supports event streaming, enabling developers to monitor real-time JVM events. This is useful for live diagnostics and performance monitoring.

7. Foreign-Memory Access API (Incubator) 🌍

The Foreign-Memory Access API provides an API to access memory outside of the Java heap, enabling higher performance when interacting with native libraries or large data sets.

8. Packaging Tool (Incubator) 📦

The jpackage tool in Java 14 lets you package Java applications into native installers, such as .exe for Windows or .dmg for Mac, making deployment easier.

9. Non-Volatile Mapped Byte Buffers 🗃️

Java 14 introduces Non-Volatile Mapped Byte Buffers, allowing Java programs to work with non-volatile memory more efficiently. Data written to non-volatile memory remains intact even after a system reboot.

Example Use Case:

For high-performance applications that need persistent data access (e.g., in-memory databases), using non-volatile memory ensures that data remains available without requiring disk writes for persistence.

Give these features a try and see how they fit into your Java projects !

Happy Coding !

Jahid Momin

--

--

Jahid Momin
Jahid Momin

Written by Jahid Momin

Team Lead | Sr Software Engineer | Spring boot | Microservices | JavaScript | CodeIgniter | HTML | CSS | ReactJS | NextJS | Youtuber | Writer

No responses yet