1
Like look again, the power is unlimited. search for "16119b19459923 16119b19459924 Program ".
This article Github.com/niumoo/JavaNotes and unread code blog has been included, there are many knowledge points and series of articles.

Java 14, 图片来自 medium.com

Java 14 was released as early as September 2019. Although it is not a long-term support version, it also brings many new features.

Java 14 official download: https://jdk.java.net/archive/

Java 14 official document: https://openjdk.java.net/projects/jdk/14/

Java 14 new features:

  • 305: instanceof type judgment (preview)
  • 343: Packaging Tools (Incubating)
  • 345: G1 supports NUMA (non-uniform memory access)
  • 358: More useful NullPointerExceptions
  • 359: Records (Preview)
  • 361: Switch expression (standard)
  • 362: Deprecated support for Solaris and SPARC ports
  • 363: Remove the CMS garbage collector
  • 364: ZGC of macOS
  • 365: ZGC for Windows
  • 366: Deprecated ParallelScavenge + SerialOld GC combination
  • 367: Delete Pack200 Tools and API
  • 368: Text block (second preview)
  • 370: Foreign-Memory Access API (Incubator)
  • 349: JFR Event Streaming
  • 352: Non-Volatile Mapped Byte Buffers

<!-- more -->

Note: If a function is a preview version, then the preview function needs to be turned on when compiling and running.

./javac --enable-preview --release 14 Test.java
./java --enable-preview Test
This article belongs to the Java new feature tutorial series, which will introduce the new features of each version of Java, you can click to browse.

1. JEP 305: Instanceof type judgment (preview)

Before Java 14, after using instanceof for type judgment, you need to perform object type conversion before you can use it.

package com.wdbyte;

import java.util.ArrayList;
import java.util.List;

public class Java14BeaforInstanceof {

    public static void main(String[] args) {
        Object obj = new ArrayList<>();
        if (obj instanceof ArrayList) {
            ArrayList list = (ArrayList)obj;
            list.add("www.wdbyte.com");
        }
        System.out.println(obj);
    }
}

In Java 14, you can specify the variable name for type conversion when judging the type, which is convenient for use.

package com.wdbyte;

import java.util.ArrayList;

public class Java14Instanceof {
    public static void main(String[] args) {
        Object obj = new ArrayList<>();
        if (obj instanceof ArrayList list) {
            list.add("www.wdbyte.com");
        }
        System.out.println(obj);
    }
}

As you can see, after using instanceof to determine that the type is established, the type is automatically forced to be the specified type.

Output result:

[www.wdbyte.com]

This feature is still a preview feature in Java 14, and it is officially corrected in Java 16.

2. JEP 343: Packaging Tool (Incubating)

In Java 14, a packaging tool was introduced. The command is jpackage . Use the jpackage command to package the JAR package into a software format supported by different operating systems.

jpackage --name myapp --input lib --main-jar main.jar --main-class myapp.Main

The common platform format is as follows:

  1. Linux: deb and rpm
  2. macOS: pkg and dmg
  3. Windows: msi and exe

It should be noted that jpackage does not support cross-compilation, which means that it cannot be packaged into the software format of macOS or Linux system on the windows platform.

3. JEP 345: G1 supports NUMA (non-uniform memory access)

The G1 collector can now sense the NUMA to improve the performance of G1. You can use +XX:+UseNUMA enable this feature.

Extended reading: https://openjdk.java.net/jeps/345

4. JEP 358: More useful NullPointerExceptions

NullPointerException has always been a relatively common exception, but before Java 14, if there are multiple expressions in a row, after NULL a null pointer, simply judging from the error message, you may not know which object is 06119b1945a1f8. Below is a demo.

package com.wdbyte;

public class Java14NullPointerExceptions {

    public static void main(String[] args) {
        String content1 = "www.wdbyte.com";
        String content2 = null;
        int length = content1.length() + content2.length();
        System.out.println(length);
    }
}

Before Java 14, we can only get the number of rows where the error occurred from the following error report, but it is not certain whether it is conteng1 or content2 for null .

Exception in thread "main" java.lang.NullPointerException
    at com.alibaba.security.astralnet.console.controller.ApiChartsTest.main(Java14NullPointerExceptions.java:8)

But in Java 14, it will tell you because "content2" is null .

Exception in thread "main" java.lang.NullPointerException: 
    Cannot invoke "String.length()" because "content2" is null
    at com.wdbyte.Java14NullPointerExceptions.main(Java14NullPointerExceptions.java:8)

5. JEP 359: Records (Preview)

record is a brand new type. It is essentially a final class, and all attributes are final . It will automatically compile public get hashcode , equals , toString etc., which reduces the amount of code writing.

Example: Write a Dog record class and define the name and age attributes.

package com.wdbyte;

public record Dog(String name, Integer age) {
}

Use of Record.

package com.wdbyte;

public class Java14Record {

    public static void main(String[] args) {
        Dog dog1 = new Dog("牧羊犬", 1);
        Dog dog2 = new Dog("田园犬", 2);
        Dog dog3 = new Dog("哈士奇", 3);
        System.out.println(dog1);
        System.out.println(dog2);
        System.out.println(dog3);
    }
}

Output result:

Dog[name=牧羊犬, age=1]
Dog[name=田园犬, age=2]
Dog[name=哈士奇, age=3]

This feature was previewed twice in Java 15 and officially released in Java 16.

6. JEP 361: Switch expression (standard)

Switch expression improvements have been started since Java 12. Java 12 enables switch to support the L-> syntax, and Java 13 introduces the yield keyword to return the result, but the functions in Java 12 and 13 are preview versions, and in Java In 14, it was officially converted.

// 通过传入月份,输出月份所属的季节
public static String switchJava12(String month) {
     return switch (month) {
        case "march", "april", "may"            -> "春天";
        case "june", "july", "august"           -> "夏天";
        case "september", "october", "november" -> "秋天";
        case "december", "january", "february"  -> "冬天";
        default -> "month erro";
    };
}
// 通过传入月份,输出月份所属的季节
public static String switchJava13(String month) {
    return switch (month) {
        case "march", "april", "may":
            yield "春天";
        case "june", "july", "august":
            yield "夏天";
        case "september", "october", "november":
            yield "秋天";
        case "december", "january", "february":
            yield "冬天";
        default:
            yield "month error";
    };
}
Extended reading: Java 12 new feature introduction , Java 13 new feature introduction , JEP 325: Switch expression

7. JEP 368: Text block (second preview)

The text block is a syntax introduced in Java 13, and it has been enhanced in Java 14. The text block is still a preview function, this update adds two escape characters.

  1. 06119b1945a536 No line \
  2. \s means a space

Example: Text block experience

String content = """
        {
            "upperSummary": null,\
            "sensitiveTypeList": null,
            "gmtModified": "2011-08-05\s10:50:09",
        }
         """;
System.out.println(content);

Output result:

{
    "upperSummary": null,    "sensitiveTypeList": null,
    "gmtModified": "2011-08-05 10:50:09",
}

text block function was officially released in Java 15.

Other updates

support for Solaris and SPARC ports 16119b1945a625

Starting from Java 14, the support for Solaris/SPARC, Solaris/x64, and Linux/SPARC ports has been abandoned, and part of the development is bound to accelerate the overall development rhythm of Java.

Related reading: https://openjdk.java.net/jeps/362

JEP 363: Remove the CMS garbage collector

Removed support for the CMS (Concurrent Mark Sweep) garbage collector. In fact, the CMS garbage collector was removed as early as Java 9, but it was officially deleted in Java 14.

JEP 364: ZGC on macOS (experimental)

Java 11 introduced the Z Garbage Collector (ZGC) on Linux, and it can now be ported to macOS.

JEP 365: ZGC on Windows (experimental)

Java 11 introduced the Z Garbage Collector (ZGC) on Linux, and it can now be ported to Windows (version greater than 1803).

JEP 366: Deprecation of ParallelScavenge + SerialOld GC combination

Because there are not many usage scenarios, the maintenance work is too large and it is discarded. Related reading: https://openjdk.java.net/jeps/366

JEP 367: Remove Pack200 tool and API

refer to

  1. https://openjdk.java.net/projects/jdk/14/
  2. https://openjdk.java.net/jeps/366
  3. https://openjdk.java.net/jeps/362
  4. Java new feature tutorial

<end>

Hello world:) I am Aran, a first-line technical tool person, and write articles seriously.

, all of them are talents, not only are they handsome and good-looking, they are also good-sounding.

The article is continuously updated. You can follow the public Program " or visit " Unread Code Blog ".

Reply [Information] There are various series of knowledge points and must-read books I prepared.

This article Github.com/niumoo/JavaNotes has been included, there are many knowledge points and series of articles, welcome to Star.

等你好久


程序猿阿朗
376 声望1.8k 粉丝

Hello world :)