Java 14 Features

Yash Walke
6 min readMar 1, 2022

--

Keeping up with the six-month cycle tradition, after the release of Java 13 on September 17, 2019, Java 14, another non-LTS version is scheduled to release on March 17, 2020.

Here’s the list of Java 14 features:

  • Switch Expressions (Standard) — JEP 361
  • Pattern Matching for instanceof (Preview) — JEP 305
  • Helpful NullPointerExceptions — JEP 358
  • Records (Preview) — JEP 359
  • Text Blocks (Second Preview) — JEP 368
  • Packaging Tool (Incubator) — JEP 343
  • NUMA-Aware Memory Allocation for G1 — JEP 345
  • JFR Event Streaming — JEP 349
  • Non-Volatile Mapped Byte Buffers — JEP 352
  • ZGC on macOS — JEP 364
  • ZGC on Windows — JEP 365
  • Foreign-Memory Access API (Incubator) — JEP 370

Java 14 Installation Setup on Mac OS

  • To get started with Java 14, download the JDK from here.
  • Copy and extract the tar file in the /Library/Java/JavaVirtualMachines as shown below:

Once that’s done, open the bash_profile using any text editor. I’m using vim ~/.bash_profile. Set the path of Java14 to JAVA_HOME, save changes and do a source ~/.bash_profile to reflect the changes.

Finally, you’re ready to compile and run programs using Java 14. We’ll be using JShell, an interactive REPL command-line tool for quickly testing the new Java 14 features.

It’s important to note that many features released in Java 14 are in preview. This means that though they’re fully working right now, things may be modified in the future. Some could be made a standard or simply removed in the next release cycles. In order to test preview features, you need to explicitly set --enable-preview when running the JShell or Java Program as shown below:

In the next few sections, let’s discuss some of the language and JVM features.

1. Switch Expressions

Switch Expressions after staying a preview feature in the last two releases –Java 12 and Java 13 have finally attained permanent status in Java 14.

  • Java 12 introduced the lambda syntax for switch expressions thereby allowing multiple case labels for pattern matching as well as preventing fall-throughs which lead to verbose code. It also enforced exhaustive cases wherein a compilation error would be thrown if all the input cases aren’t covered.
  • Java 13, the second preview introduced yield statements instead of break returning values from an expression.

Java 14 has finally made these features a standard now.

Note: Yield isn’t a new keyword in Java. It’s just used in switch expressions.

2. Pattern Matching for instanceof (Preview)

Ask any Java developer to show their codebase and you’ll a good use of instanceof conditions throughout the code. Specifically, an instanceof conditional check is generally followed by a typecasting.

Java 14, gets rid of this verbosity by making conditional extraction a lot more concise.

Before Java 14:

Java 14 Onwards:

In the above code, the instance jd would be only assigned if obj is of type Journaldev. The scope of the variable is limited to the conditional block only.

3. Helpful NullPointerExceptions

Null Pointer Exceptions are a nightmare for any developer. Previously, until Java 13, it was tricky to debug the infamous NPEs. Developers had to fall onto other debugging tools or manually figure the variable/method that was null since the stack trace would only show the line number.

Before Java 14:

Java 14 introduced a new JVM feature that gives better insights with a more descriptive stack as shown below:

Note: The above feature is not a language feature. It’s an enhancement in the runtime environment.

4. Records (Preview)

A record is a data class that stores pure data. The idea behind introducing records is to quickly create simple and concise classes devoid of boilerplate code.

Normally a class in Java would require you to implement equals(), hashCode() , the getters and setters methods. While some IDEs support auto-generation of such classes, the code is still verbose. With a record you need to simply define a class in the following way.

The Java compiler will generate a constructor, private final fields, accessors, equals/hashCode and toString methods automatically. The auto-generated getter methods of the above class are name() and topic().

To look into the generated code, use javap Author after you’ve compiled the program using javac. The following illustration shows the generated class for record Author (String name, String topic) {}:

Javap Records Java 14

The semantics of Records is similar to Data Classes in Kotlin

Furthermore, we can add additional fields, methods, and constructors to the record in the following way:

The additional constructor defined inside the record is called a Compact constructor. It doesn’t consist of any parameters and is just an extension of the canonical constructor.

A compact constructor wouldn’t be generated as a separate constructor by the compiler. Instead, it is used for validation cases and would be invoked at the start of the main constructor.

Few important things to note about Records:

  • A record can neither extend a class nor it can be extended by another class. It’s a final class.
  • Records cannot be abstract
  • Records cannot extend any other class and cannot define instance fields inside the body. Instance fields must be defined in the state description only
  • Declared fields are private and final
  • The body of a record allows static fields and methods

4.1) Values Inside Reference Fields Of A Record Can Be Mutated

It’s important to note that for fields defined which are objects, only the reference is immutable. The underlying values can be modified. The following illustration shows a record in which the ArrayList is modified. As you can see, the value is modified whenever the ArrayList is changed.

4.2) Records Can Implement Interfaces

The following code shows an example of implementing an interface with records:

Here’s the output of the above code in action in a JShell:

Java 14 Records With Interface

4.3) Records support multiple constructors

Records allow declaring multiple constructors with or without parameters as shown below:

4.4) Records Allow Modifying Accessor Methods

Though records do generate public accessor methods for the fields defined in the state description, they also allow you to redefine the accessor methods in the body as shown below:

4.5) Check Record and its Components at Runtime

Records provide us with isRecord() and getRecordComponents() to check if the class is a record and also look into its fields and types. The following illustration shows how it is done:

While we did add additional fields and methods to the record in the above code examples, make sure you don’t overdo this. Records are designed as plain data carriers and if you’re looking to implement a lot of additional methods, it’s better to fall back onto the normal class.

5. Text Blocks (Preview)

Text Blocks were introduced as a preview feature in Java 13 with the goal to allow easy creation of multiline string literals. It’s useful in easily creating HTML and JSON or SQL query strings.

In Java 14, Text Blocks are still in preview with some new additions. We can now use:

  • Backslash for displaying nice-looking multiline string blocks.
  • \s is used to consider trailing spaces which are by default ignored by the compiler. It preserves all the spaces present before it.

--

--

Yash Walke

I'm a tech guy, who engaged with codes. Loves to spread knowledge.👨‍💻.