Sorry, you need to enable JavaScript to visit this website.

Simplifying Java14 features

Simplifying Java14 features
September 27, 2021

Every six months, Oracle releases a new Java development kit (JDK), making Java one of the fast-moving languages. The current release is JDK 15, but the major release was JDK 14, announced in March 2020. Here is the list of new features that Java introduced in JDK 14 that we can use in our day-to-day code to improve productivity.

Below is a list of key features with their JDK enhancement proposal (JEP):

  1. [JEP-359] Records

    This feature provides a compact syntax for declaring classes which hold shallowly immutable data. This feature can greatly reduce boilerplate code in classes of this type, but the biggest benefit allowing the modeling of data as data. It should be easy, clear, and concise to declare these shallowly immutable nominal data aggregates.

    Records are a special kind of class that stores only data. These are immutable classes that reduce repetitive code used in every POJO class and improve readability. 

    Records are special kind of class which stores only data and its purpose is to reduce repetitive code

    Example: 

    Let’s take an example of a Student class which has attributes id and name.

    Data model code for a Student class can be defined simply by:

    record Student (String name, int id) {};

    Java compiler will automatically generate constructor, equals and hash code, toString, and getters for “name” and “id” fields. Since class is immutable, no setters will be created.

    Further, we can add additional fields, constructors, and methods:

    record Student (String name, String id) {
    static long phone;
    public static String getPhone() {
    return phone;
    }
    public Student(int id) {
    if(id return throw new IllegalArgumentException("Incorrect Id");
    }
    };

    *Important points* for Java Record are:

    • Java Records are final, so they cannot be extended. Nor can they extend any class
    • Records can implement interfaces
    • Fields defined are all private and final
    • Records cannot be abstract
  2. [JEP-361] Switch expressions

    This feature allows switch to be used either as a statement or an expression.

    Before JDK 14, switch case could only be used like:

    Java

    Now, with the use of lambda (which was introduced in JDK 8), switch case can be modified as below:

    Java

  3. [JEP-368] Text blocks

    This feature enhances the readability of long string or multiline strings. It also helps in reducing errors in formatting the string.

    Text blocks in Java have kept on evolving with subsequent releases of the Java development kit. The latest text block has two new escape sequences.

    \ : This indicates the end of the line but does not add a new line character
    \s : It represents single spaces

    Example:

    Java

    OUTPUT:
    Hello Sir,
    How are you?

    Another example:

    Before Java 14, string would be written as:

    Java

    Whereas now, with Java 14, the above string can be written like below. This makes the string readable even for non-java programmers and reduces formatting errors.

  4. [JEP-305] Pattern matching for instanceof 

    This feature eliminates the need for explicit type cast, thus reducing the boiler plate code.

    Before JDK14, the code used to be written as:

    Java

    After the release of Java development kit JDK14, the above code can be refactored as:

    Java

    Variable stud will only be assigned if “obj” is an object of class Student. The scope of stud is limited to conditional block only.

  5. [JEP-358] Helpful NullPointerExceptions

    There are slight improvements in production-related features, such as NullPointerException (NPE).

    Every developer must have faced null pointer exceptions in code or production-ready code. To get to the root cause, the developer had to go through series of debugging cycles to figure out the exact point that resulted in NPE.

    With this feature, the developer can accurately identify which variable was null as JDK14 has a more insightful stacktrace of exception messages.

    Example:  NPE occurred in the code below:

    Before JDK14: Exception stacktrace was as given below, and the developer could not identify which variable caused NPE.

       Exception in thread "main" java.lang.NullPointerException

       at NullPointerExample.main(NullPointerExample.java:10)

    With JDK 14, stacktrace is more informative, and tells the point exactly where the exception occurred.

       Exception in thread "main" java.lang.NullPointerException: Cannot invoke   "Subject.getTecher()" because the return value of " Student.getSubject()" is null at NullPointerExample.main(NullPointerExample.java:8)

  6. JVM hotspot features 

    • [JEP 365 for Windows and JEP 364 for macOS ] ZGC supports more platforms

      Z garbage Collector was introduced in Java 11 as an experimental feature for only Linux and X64. ZGC is a scalable and low latency GC. Since it became very popular, Oracle has now implemented ZGC for Windows and macOS as well. But this is still an experimental feature and has not become production-ready yet.

    • [JEP-345] NUMA

      Non-uniform Memory access (NUMA) runs singleton JVM across multiple sockets, but this technology was available only for the parallel collector. This JEP improves the performance of the G1 garbage collector for non-uniform memory access (NUMA) systems.

  7. Incubating features
    • [JEP-343] Packaging tool

      Allows developers to package their java applications for distribution in platform specific format. This tool is introduced in an incubator module, which is a way of putting non-final APIs and non-final tools in the hands of developers to get their feedback while the APIs/tools progress towards either finalization or removal in a future release.

      Earlier, to provide executable code, a developer must package their code into jar files and then run it in a Java executable environment. But now, with the help of this tool, a developer can convert their code into a lightweight image that can run as an executable file by just double-clicking on it, just like we deploy any installer in our windows or mac machine.

    • [JEP 370] Foreign memory access API

      FMA permits Java code to access foreign memory, such as native memory, which is outside of the heap safely and efficiently.

  8. Features deprecated and removed

    [DEPRECATED] ParallelScavenge + SerialOld GC Combination -  as it is hardly used and maintenance efforts in very release is high.
    [DEPRECATED] Solaris and SPARC Ports - as it was being used for a long time.
    [REMOVED] Concurrent Mark Sweep (CMS) Garbage Collector got deprecated in Java 9 and is now removed in Java 14. G1 GC is not commonly used and is much more efficient than CMS.
    [REMOVED] Pack200 Tools and API were deprecated in Java 11 and removed in Java 14.

References

https://www.oracle.com/java/technologies/javase/14-relnote-issues.html

https://www.oracle.com/corporate/pressrelease/oracle-announces-java14-031720.html

https://www.baeldung.com/java-14-new-features

More from Sunil Siddharth