Make writing a habit together! This is the 10th day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details.

Java8 was released on March 18, 2014. As of April 6, 2022, the current latest release is Java18. Versions 17, 11, and 8 are the long Term Support (LTS) versions currently supported. This article takes you through the features of each version of Java from 8 onwards. Sit back and go! Want to have a crush on an article, click here to interview the kill | please discuss Java8-18 of the new features introduced (4)

New features in Java 14

Records

Records is a restricted class form, which is ideal for POJOs. A standard data carrier class will have some private fields as well as constructors and getters/setters.

Let’s use Java 8 syntax to create an example of a simple data carrier class with two members:

public class Location { double x; double y; public Location(double x, double y) { this.x = x; this.y = y; } public double getX() { return x; } public double getY() { return y; }}Copy the code

We can override the above class with Record using the code given below:

record NewLocation(double x, double y) {}
Copy the code

Record gets getters and constructors at run time, as well as equals (), HashCode (), and toString () methods.

Helpful Nullpointerexception

Until then, the stack trace for NullPointerException doesn’t have much to say about it, except that a value for a row in a given file is NULL.

While this information is useful, it suggests just one line of code to debug, rather than just looking at the logs for developers to understand.

Java has now made this process easier by adding the ability to indicate exactly what is NULL in a given line of code.

Here’s an example of a null pointer:

int[] arr = null;
arr[0] = 1;
Copy the code

In previous versions, when running this code, the log would say:

Exception in thread "main" java.lang.NullPointerException
at com.baeldung.MyClass.main(MyClass.java:27)
Copy the code

But for Java 14, the log prints:

java.lang.NullPointerException: Cannot store to int array because "a" is null
Copy the code

As we can see, we now know exactly which variable is causing the exception.

Text Blocks

Text blocks have received another upgrade over Java 13, and now have two new escape sequences, but are still previews.

  • : represents the end of a line so that no new line characters are introduced
  • \ S: indicates a single space

Here’s an example:

String multiline = "A quick brown fox jumps over a lazy dog; the lazy dog howls loudly.";
Copy the code

It can now be written as:

String multiline = """
    A quick brown fox jumps over a lazy dog; \
    the lazy dog howls loudly.""";
Copy the code

This improves the readability of the string.

There are other new features, including but not limited to:

  • Packaging Tool
  • Foreign Memory Access API
  • ZGC on Windows
  • NUMA-Aware Memory Allocation for G1
  • JFR Event Streaming

To continue, the following will continue to talk about the new features of each version, stay tuned!