In this article we will learn about New Features in Java 14 along with deprecated/removed features and APIs.
S.No. | Feature / API | Deprecated Since | Removed Since |
---|---|---|---|
1. | Solaris and SPARC Ports | 14 | |
2. | ParallelScavenge + SerialOld GC Combination | 14 | |
3. | CMS GC | 9 | 14 |
4. | Pack200 Tools and API | 11 | 14 |
5. | Nashorn JavaScript Engine | 11 | |
6. | Java FX (moved to OpenJFX) | 11 | |
7. | Java EE and CORBA modules | 9 | 11 |
8. | javah Native-Header Generator | 10 | |
9. | jhat Heap Visualizer | 9 | |
10. | Launch-Time JRE Version Selection | 9 | |
11. | Rarely-Used GC Combinations | 8 | 9 |
12. | Applet API | 9 |
We can download Java 14 from Oracle official website : https://jdk.java.net/14/
In this section we will discuss Java 14 features in detail with the help of examples.
In Java 14, instanceof operator has been modified to have type test pattern. A type test pattern (used in instanceof) consists of a predicate that specifies a type, along with a single binding variable.
Instanceof is used to check whether the object reference is and instance of given Type or not, and return boolean flag. For example:
if (obj instanceof String) { String str = (String) obj; // need to declare and cast again the object .. str.contains(..) .. }else{ str = .... }
if (!(obj instanceof String str)) { .. str.contains(..) .. // no need to declare str object again with casting } else { .. str.... }
In above example, the instanceof operator “matches” the target obj to the type test pattern if obj is an instance of String, then it is cast to String and assigned to the binding variable str.
Note: that the pattern will only match, and str will only be assigned, if obj is not null.
In Java 14, a text block is a multi-line string literal. It means we do not need to think about line terminators, string concatenations, and delimiters as we used to write the normal string literals in Java pervious versions.
In Java, to embed HTML, XML, SQL, or JSON snippet into a code is often hard to read and hard to keep, and to overcome this problem, Java 14 has introduced Text Block feature.
A text block contains zero or more content characters, which are enclosed by open and close delimiters
Example: Text Block
String str = "The Basic"; String textBlock = """ Tech Info"""; String concatStrings = str + textBlock; System.out.println(concatStrings);
Result (Output):
The Basic Tech Info
String html = "<html>\n" + " <body>\n" + " <p>The Basic Tech Info</p>\n" + " </body>\n" + "</html>\n";
String html = """ <html> <body> <p>The Basic Tech Info</p> </body> </html> """;
This is very helpful feature for developer. In Java 14, NullPointerExceptions exception message generated by JVM improved. We will see in below working example.
Note: We need to pass -XX:+ShowCodeDetailsInExceptionMessages JVM flag to enable this feature while running the application.
public class HelpfulNullPointerException { public static void main(String[] args) { Employee e = null; System.out.println(e.getName()); } }
On Console – Error message with improved detail as below.
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "com.the.basic.tech.info.Employee.getName()" because "e" is null at com.the.basic.tech.info.HelpfulNullPointerException.main (HelpfulNullPointerException.java:9)
However there are some Risk. The null-detail message may contain variable names from the source code in Production. Exposing this information might be considered as security risk.
This is a preview language feature in JDK 14. It is used to compact the class declaration syntax with record.
We need to write a lot of low-value, repetitive code to write a simple data carrier class responsibly: constructors, accessors, equals(), hashCode(), toString(), etc. To avoid this repetitive code, Java is planned to use record. For example:
final class Scale { public final int x; public final int y; public Scale(int x, int y) { this.x = x; this.y = y; } // state-based implementations of equals, hashCode, toString // nothing else
record Scale(int x, int y) { }
In Java 14, record eliminates all the code needed to set and get the data from instance. Records transfer this responsibility to java compiler which generates the constructor, field getters, hashCode() and equals() as well toString() methods.
//Arrow labels static void grade(int g) { System.out.println( switch (g) { case 1 -> "A"; case 2 -> "B"; default -> "C"; } ); } ------------------------------------------ //Yielding a value - introduce a new yield int j = switch (day) { case MONDAY -> 0; case TUESDAY -> 1; default -> { int d = day.toString().length(); int result = f(d); yield result; } };
switch (day) { case MONDAY: case FRIDAY: case SUNDAY: System.out.println(6); break; case TUESDAY: System.out.println(7); break; case THURSDAY: case SATURDAY: System.out.println(8); break; case WEDNESDAY: System.out.println(9); break; }
switch (day) { case MONDAY, FRIDAY, SUNDAY -> System.out.println(6); case TUESDAY -> System.out.println(7); case THURSDAY, SATURDAY -> System.out.println(8); case WEDNESDAY -> System.out.println(9); }
In JDK 8, a tool called javapackager was released as part of the JavaFX kit. However, after JavaFX split from Java with the release of JDK 11, the popular javapackager was no longer available.
The jpackage tool bundles a Java application into a platform-specific package containing all the dependencies required. As a set of ordinary JAR files or as a collection of modules. The supported platform-specific package formats are:
The tool can be invoked directly, from the command line, or programmatically, via the ToolProvider API as below.
$ jpackage --name microserviceapp --input lib --main-jar main.jar
Note: CMS GC has been removed from Java 14. It was marked as deprecated in Java 9 (JEP 291). It will be available till Java 13 only.
Drop me your questions in comments related to Java 14 new features. Keep exploring new things 🙂