Interview to kill | Please talk about the new features introduced by Java8-18 (5)

Get into the habit of writing together! This is the 10th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

Java8 was released on March 18, 2014, and as of April 6, 2022, the latest release is Java18. Versions 17, 11 and 8 are the currently supported Long Term Support (LTS) releases. This article leads you to review the features of each version starting from Java 8. Sit on the bench and go! If you want to read the previous article, click here for an interview and anti-kill | Please talk about the new features introduced by Java8-18 (4)

What's New in Java 14

Records

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

Let's create an example of a simple data carrier class with two members using Java 8 syntax:

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;
    }
}
复制代码

We can rewrite the above class using Record using the code given below:

record NewLocation(double x, double y) {}
复制代码

Record will get getters and constructors at runtime, and will also get equals(), hashcode(), and toString() methods.

Helpful Nullpointerexception

Until then, the stack trace for NullPointerException doesn't have much to say, except that a certain value on a line in a given file is null.

While this information is useful, they are only suggestions for debugging a line of code, not just looking at the logs for the developer to understand.

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

Here's an example of a null pointer:

int[] arr = null;
arr[0] = 1;
复制代码

In previous versions, when running this code, the log showed:

Exception in thread "main" java.lang.NullPointerException
at com.baeldung.MyClass.main(MyClass.java:27)
复制代码

But for Java 14, the log prints:

java.lang.NullPointerException: Cannot store to int array because "a" is null
复制代码

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

Text Blocks

Text blocks have received another upgrade from Java 13 and now have two new escape sequences, but are still a preview feature.

  • : indicates end of line, so as not to introduce new line characters
  • \s: represents a single space

for example:

String multiline = "A quick brown fox jumps over a lazy dog; the lazy dog howls loudly.";
复制代码

It can now be written as:

String multiline = """
    A quick brown fox jumps over a lazy dog; \
    the lazy dog howls loudly.""";
复制代码

This improves the readability of strings.

Of course 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 be continued, let's continue to talk about the new features of each version, so stay tuned!

Guess you like

Origin juejin.im/post/7084698526026432548