Java 14 was officially released on March 17, 2020, but many companies are still using Java 7 or Java 8, and they get nervous every time a new version of Java is released. This is not an LTS (long term support) version, so don’t panic. Let’s check it out and use it when the LTS version comes out.

Release notes

The current Java release rhythm is two General Availability (GA) releases per year, with releases every six months in March and September.

Oracle only provides paid support for LTS releases, including Java 8 and Java 11. Paid support for Java 11 will continue until 2026, with LTS releases every three years. The next LTS release will be Java 17.

The rapid update of the Java version is partly to cater to the trend of developers and current technology development, but also to facilitate the delivery of the Java version, the large version is broken down into small versions, at least there will not be a big delay.

New features in Java 14

This release contains more JEP (Java/JDK Enhancement Proposals) than Java 12 and Java 13 combined, 16 new features in total.

  • 305: Instanceof Pattern Matching (Preview)
  • 343: Packaging Tool (Incubator)
  • 345: NUMA memory allocation optimization for G1
  • 349: JFR event flow
  • 352: Non-atomic byte buffer mapping
  • 358: Friendly null pointer exception
  • 359: Records (Preview)
  • 361: Switch expressions (standard)
  • 362: Deprecates Solaris and SPARC ports
  • 363: Removes the CMS (Concurrent Mark Sweep) garbage collector
  • 364: ZGC on macOS
  • 365: ZGC on Windows
  • Avenge + SerialOld GC
  • 367: Remove the Pack200 Tools and API
  • 368: Text Block (second preview)
  • 370: External Memory API (Incubator)

As you can see, many of the optimizations were about the garbage collector, and the CMS garbage collector is finally out of the way, with Java 9 marked as obsolete and Java 14 officially removed.

Configure Idea to run Java 14

1. Download the latest Idea

Idea preview 2020.1 supports Java 14, so it’s best to download and install this version. Screenshot below:

2. Configure the Idea

If you only download the latest Idea to run Java 14, the following error will be displayed:

Error:(10, 35) Java: Pattern matching in Instanceof is a preview feature, disabled by default. (Use — enable-Preview to enable pattern matching in Instanceof)

This is because many of the features in Java 14 are still in the preview release, and you need to configure Idea to support it (Java 14) preview function, as shown in the following figure:

Tip: If you don’t want to use Idea to experience new features, you can also use javac to compile code and run it with the “–enable-preview” parameter, since many features in Java 14 are available in preview. The full compilation command “javac –enable-preview –release 14 xxx.java”.

Code experiences new features

We’re going to use code to demonstrate some of the most important and useful new features in Java 14, and we’re going to do it in a way that compares old and new code.

Instanceof convenience

private static void instanceofTest(a) {
    Object obj = "Java Chinese Community";
    / / the old way
    if (obj instanceof String) {
        String s = (String) obj;
        System.out.println(s);
    }
    / / a new way
    if (obj instanceofString s) { System.out.println(s); }}Copy the code

The execution results of the above programs are as follows:

Java Chinese Community Java Chinese Community

2. Convenient Switch

/ / the old way
switch ("java") {
    case "java":
    case "jdk":
        System.out.println("This is Java.");
        break;
    default:
        System.out.println("default");
        break;
}
/ / a new way
switch ("java") {
    case "java"."jdk" -> System.out.println("This is Java.");
    default -> System.out.println("default");
}
Copy the code

The execution results of the above programs are as follows:

This is Java. This is Java.

3. Add Records

// Record type
record People(Integer id, String name, Integer age) {}/ / instantiate
People people = new People(1."Wang".18);
// Output attribute name
System.out.println(people.name);
Copy the code

The execution results of the above programs are as follows:

Lao wang

Let’s use a decompiler to look at the final implementation code for Records:

➜ example git:(master) qualify javap Java14Example\$1People
final class com.example.Java14ExampleThe $1People extends java.lang.Record {
  public com.example.Java14Example$1People(java.lang.Integer, java.lang.String, java.lang.Integer);
  public java.lang.String toString(a);
  public final int hashCode(a);
  public final boolean equals(java.lang.Object);
  public java.lang.Integer id(a);
  public java.lang.String name(a);
  public java.lang.Integer age(a);
}
Copy the code

The People class eventually inherits the Record class, creating methods toString(), equals(), hashCode(), and three custom property methods. Two lines of code is cool, but there are a few details: The Record type is final, so it can’t have subclasses, and the class can’t inherit from any other parent because it already inherits from the Record class.

conclusion

Java 14 provides several useful syntax features, such as instanceof alignment and assignment, as well as concise Switch and Records. However, the Records feature is also a preview feature, which means it will probably be removed in future releases. It is not a complete replacement for Lombok, as it inherits Record and is limited by the final keyword. Expect more surprises in the next release.

Reference & Acknowledgements official log: jdk.java.net/14/release-…

Follow the qr code below and subscribe for more exciting content.