This is the fourth day of my participation in the August More text Challenge. For details, see: August More Text Challenge
Don’t do the title party, seriously write an article. The article has been published at Github.com/niumoo/Java… And unread code blog, point attention, don’t get lost.
Java 14 was released back in September 2019, and while it’s not a permanent support version, it also brings a number of new features.
Java 14 official download: jdk.java.net/archive/
Java 14 official documentation: openjdk.java.net/projects/jd…
Java 14 new features:
- 305: Instanceof Type judgment (Preview)
- 343: Packing Tool (Incubation)
- G1 supports NUMA (non-uniform memory access)
- 358: More useful NullPointerExceptions
- 359: Records (Preview)
- 361: Switch expression (standard)
- 362: Discards support for Solaris and SPARC ports
- 363: removes the CMS garbage collector
- 364: ZGC macOS
- 365: Windows ZGC
- Avenge: ParallelScavenge + SerialOld GC
- 367: delete the 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
Note: If a feature is a preview, the preview feature needs to be turned on at compile and run time.
./javac --enable-preview --release 14 Test.java
./java --enable-preview Test
Copy the code
This article is part of the Java New Features tutorial series, covering the new features in each version of Java.
1. JEP 305: Instanceof Type Judgment (Preview)
Prior to Java 14, after using Instanceof for type determination, object type conversion was required.
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); }}Copy the code
In Java 14, you can specify the name of the variable to be cast when determining the type.
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); }}Copy the code
As you can see, after using instanceof to determine that the type is true, the type is automatically cast to the specified type.
Output results:
[www.wdbyte.com]
Copy the code
This feature was previewed in Java 14 and is officially available in Java 16.
2. JEP 343: Packaging Tool (Incubation)
In Java 14, packaging tools were introduced with the command jpackage, which allows you to package JAR packages into software formats supported by different operating systems.
jpackage --name myapp --input lib --main-jar main.jar --main-class myapp.Main
Copy the code
Common platform formats are as follows:
- Linux:
deb
andrpm
- macOS:
pkg
anddmg
- Windows:
msi
andexe
Note that JPackage does not support cross-compilation, meaning that it cannot be packaged into macOS or Linux software formats on Windows platforms.
3. JEP 345: G1 supports NUMA (non-uniform memory access)
The G1 collector can now sense NUMA memory allocation to improve G1 performance, which can be enabled using +XX:+UseNUMA.
Further reading: openjdk.java.net/jeps/345
JEP 358: More useful NullPointerExceptions
NullPointerException is always a common exception, but before Java 14, if you had multiple expressions on a row, you might not know which object was NULL simply because of the error message. Here is an example.
package com.wdbyte;
public class Java14NullPointerExceptions {
public static void main(String[] args) {
String content1 = "www.wdbyte.com";
String content2 = null;
intlength = content1.length() + content2.length(); System.out.println(length); }}Copy the code
Prior to Java 14, we could only get the number of lines where the error occurred from the following error, but it was not clear whether conteng1 or content2 was null.
Exception in thread "main" java.lang.NullPointerException
at com.alibaba.security.astralnet.console.controller.ApiChartsTest.main(Java14NullPointerExceptions.java:8)
Copy the code
In Java 14, however, it clearly tells you that 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)
Copy the code
5. JEP 359: Records (Preview)
Record is a new type. It is essentially a final class, and all properties are final. It automatically compiles public get HashCode, equals, toString, and so on, reducing the amount of code to write.
Example: Write a Dog Record class that defines the name and age attributes.
package com.wdbyte;
public record Dog(String name, Integer age) {}Copy the code
The use of Record.
package com.wdbyte;
public class Java14Record {
public static void main(String[] args) {
Dog dog1 = new Dog("Sheepdog".1);
Dog dog2 = new Dog("Farm dog".2);
Dog dog3 = new Dog("Husky".3); System.out.println(dog1); System.out.println(dog2); System.out.println(dog3); }}Copy the code
Output results:
Dog[name= Dog, age=1] Dog[name= Dog, age=2] Dog[name= Dog, age=3]Copy the code
This feature was previewed in Java 15 and released in Java 16.
6. JEP 361: Switch Expressions (standard)
The Switch expression improvements started in Java 12. Java 12 enabled Switch to support the L-> syntax. Java 13 introduced the yield keyword to return results, but the functionality is in preview in Java 12 and 13. In Java 14, it’s official.
// Output the season of the month
public static String switchJava12(String month) {
return switch (month) {
case "march"."april"."may" -> "Spring";
case "june"."july"."august" -> "Summer";
case "september"."october"."november" -> "Autumn";
case "december"."january"."february" -> "Winter";
default -> "month erro";
};
}
// Output the season of the month
public static String switchJava13(String month) {
return switch (month) {
case "march"."april"."may":
yield "Spring";
case "june"."july"."august":
yield "Summer";
case "september"."october"."november":
yield "Autumn";
case "december"."january"."february":
yield "Winter";
default:
yield "month error";
};
}
Copy the code
Java 12 New Features, Java 13 New features, JEP 325: Switch Expressions
7. JEP 368: Text block (second preview)
Text blocks are a syntax introduced in Java 13 and enhanced in Java 14. Text blocks are still previews, but this update adds two escape characters.
\
No line breaks at the end\s
Represents a space
Example: Text block experience
String content = """{"upperSummary": null,\ "sensitiveTypeList": null, "gmtModified":"2011- 08 -05\s10:50: 09, "}""";
System.out.println(content);
Copy the code
Output results:
{
"upperSummary": null, "sensitiveTypeList": null,
"gmtModified": "2011-08-05 10:50:09",
}
Copy the code
The text block feature was released in Java 15.
Other updates
JEP 362: Disuse support for Solaris and SPARC ports
Starting with Java 14, the abandonment of support for Solaris/SPARC, Solaris/ X64, and Linux/SPARC ports, and the abandonment of some development, will certainly accelerate the overall development pace of Java.
Related: openjdk.java.net/jeps/362
JEP 363: Remove the CMS garbage collector
– Removing support for the Concurrent Mark Sweep (CMS) garbage collector, which has been removed since Java 9, was officially removed in Java 14.
JEP 364: ZGC on macOS (experimental)
Java 11 introduced the Z Garbage collector (ZGC) on Linux, and it is now portable to macOS.
JEP 365: ZGC on Windows (experimental)
Java 11 introduced the Z Garbage collector (ZGC) on Linux, and it is now portable to Windows (version over 1803).
JEP 366: Apply to ParallelScavenge + SerialOld GC
Because the use scenario is not many, maintenance work is too large, abandoned. Related: openjdk.java.net/jeps/366
JEP 367: Removes Pack200 tools and apis
reference
- Openjdk.java.net/projects/jd…
- openjdk.java.net/jeps/366
- openjdk.java.net/jeps/362
- A tutorial on new Java features
Hello world 🙂
I am A lang, the month lang wind clear Lang, a daily move brick technical tool person.
If you want to subscribe, you can follow the official “Unread Code” account, or the unread Code blog.
This article has also been posted at GitHub.com/niumoo/Java… Welcome to Star.