Java 14 Features – ‘Coz you must Expect the Unexpected from Java

Oracle announced the general availability of Java (JDK 14) on 17th March, 2020. Also, Oracle has proved its commitment of six month feature release by bringing innovation and elevation for more than two years.

Oracle Java 14 has added various new features to provide productivity and predictability to developers and enterprises.

Various JDK Enhancement Proposals (JEP) have been made along with three preview features (a feature that is not complete, but is made available before it’s officially in a release so customers can get early access and provide feedback).

These enhancements range from Java language support to new APIs for continuous monitoring of JDK Flight Recorder data and many more. Major enhancements like Foreign-Memory Access API Enhancements and Pattern Matching and Records will help Project Panama and Project Amber respectively.

Java 14 Features

The list of all the new features under Java 14 is as follows:

1. JEP305: Pattern Matching for instanceof (Preview)

This enhancement provides pattern matching for instanceof method. Instanceof is used to check whether the refrenced object is an instance of given type. The enhancement makes the code much simpler.

Dive a little deep into the concepts of Java Methods with Techvidvan.

For Example:

Before:

if (a instanceof String) {
    String str = (String) a; // need to declare and cast again the object(a)
    .. str.contains(….) ..
}else{
    str = ......
}

After:

if (!(a instanceof String str))
{
   .. str.contains(….) .. // no need to declare str object(a) again with casting
}
else {
   str…..
}

Efficient expression is thus given.

Follow TechVidvan on Google & Stay updated with latest technology trends

2. JEP 352: Non-volatile Mapped Byte Buffers

Java 14 has enhanced the MappedByteBuffer to load a portion of file data in ROM, Flash memory, hard disks (that is, any form of non-volatile memory where even if power is turned off the data remains intact.)

3. JEP 358: NullPointerExceptions

Null pointer exceptions are difficult to catch and recover in a program. Thus Java 14 makes it easier and efficient to catch Null Pointer Exceptions by generating a more clear message when they occur.

For Example: NullPointerException occurs here :

a.b.c.i = 99;


Exception in thread "main" java.lang.NullPointerException:
        Cannot read field 'c' because 'a.b' is null.
    at Prog.main(Prog.java:5)

4. JEP 361: Switch Expressions (Standard)

Switch expressions was a preview feature in JDK 12 and JDK 13 which has now been extended in Java 14. In this switch can be used as an expression rather than a statement with the help of an ->. Switch can also return a value in Java 14.

For Example:

int numLetters = switch (month) {
    case JANUARY, MARCH, APRIL -> 6;
    case FEBRUARY -> 7;
    case MAY, JUNE, JULY, AUGUST -> 8;
    case SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER -> 9;
};

5. JEP 345: NUMA – Aware Memory Allocation for G1

Non uniform memory access is a way by which the cluster of microprocessors is configured in a multiprocessor system. This is done in order to share the memory locally. This, in turn, improves the ability, efficiency and performance of the system.

Java 14 has thus provided NUMA – Aware Memory Allocation which would be helpful as it will improve the G1 performance of large machines.

6. JEP 349: JFR Event Screening

Java 14 provides an API that will monitor in-process and out-of-process events. Thus event screening will be done simultaneously with non streaming with an overhead of less than 1 percent.

The functionality of subscribing to events asynchronously will be delivered by extending the jdk.jfr.consumer package in jdk.jfr module

7. JEP 359: Records (Preview)

It is still a preview feature in Java 14. This record keyword will avoid repetition of data. Repetition of code e.g. in constructors, tostring() etc. will be reduced as record will compact the syntax of class declaration.

For Example:

Before:

final class Point {
    public final int x;
    public final int y;
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    // state-based implementations of equals, hashCode, toString
    // nothing else

After:

record Point(int x, int y) { }

8. JEP 343: Packaging Tool (Incubator)

A packaging tool undoubtedly eases out the process of installation. Applications can’t be run only through jar files they indeed need an installable kit.

This jpackage tool gathers and bundles java application into platform specific package format that contains all the dependencies required. The supported format under various platform specific package are mentioned below:

Linux: deb and rpm

macOS: pkg and dmg

Windows: msi and exe

HOLD ON! It’s the right time to take a quick revision on Java Packages to clear your basics with Techvidvan.

9. Deprecate Solaris and SPARC ports

The development of new features will be accelerated as the JDK community will be able to move the platform forward. The ports will be deprecated and configuration of such ports will generate error but it will be suppressed and preceded.

10. Remove the Concurrent Mark Sweep (CMS) Garbage Collector

Concurrent Mark Sweep garbage collector has been removed in this version. A warning message will be printed in attempt to use it. This update thus deactivates CMS compilation, all CMS – only options. The source tree will not contain CMS directory under this upgrade.

11. JEP 364 – ZGC on macOS

This enhancement proposal offers Z garbage Collector in macOS for e.g. freeing unused device memory. The ZGC under macOS has following implementations:

  • Support for multi-mapping memory on macOS.
  • Support in ZGC for dis-contiguous memory reservations.

12. JEP 365 – ZGC garbage collector on Windows

JEP 365 is same as JEP 364. It offers Z garbage Collector for Windows. Its implementations are:

  • To support for multi-mapping memory
  • Support for mapping paging-file backed memory into a reserved address space
  • Support for mapped and unmapped random parts
  • To support for committed and uncommitted random parts

13. JEP 366 – Deprecate the ParallelScavenge + SerialOld GC Combination

JEP 366 includes garbage collector. As a result any intentional usage of options involving UseParallelOldGC will show a deprecate warning.

14. JEP 367: Remove the Pack200 Tools and API

3 types of JAR file compression scheme is to be removed. They are packing and unpacking tools and APIs implemented by JSR 200 in Java SE 5.0:

  • java.util.jar.Pack200
  • java.util.jar.Pack200.Packer
  • java.util.jar.Pack200.Unpacker

15. JEP 368: Text Blocks (Second Preview)

The text blocks in Java 14 will make it easier to read and keep the code of HTML, XML, SQL, JSON snippets in java. Java 14 (second preview) has two more escape sequences that is, (line-terminator) \ and (single space) /s unlike its first preview in Java 13.

16. JEP 370: Foreign-Memory Access API (Incubator)

Java APIs do not have an apt way of accessing foreign memory. Thus JEP 370 API in Java 14 will help applications to access foreign memory even outside the java heap effectively and efficiently.

Summary

Here we come to the end of our article. In this article, we explored various new features of Java 14. These innovations and enhancements by oracle will help enterprise as well as developers community to build better applications with newer capabilities.

These will prove to be a testament to all the work that has been put in the revolutionary projects.

Thank you for reading our article. If you have any doubts related to Java 14 features, do let us know by dropping a comment below in the comment box.

Keep Learning 🙂

If you are Happy with TechVidvan, do not forget to make us happy with your positive feedback on Google | Facebook


Leave a Reply

Your email address will not be published. Required fields are marked *