Tuesday 26 May 2020

Java 14 Features with examples

Java 14, another non-LTS version has been released on March 17, 2020. Let's discuss it's feature with examples.

Here’s the list of Java 14 features:

  • Pattern Matching for instanceof (Preview) – JEP 305
  • Switch Expressions (Standard) – JEP 361
  • Helpful NullPointerExceptions – JEP 358
  • Records (Preview) – JEP 359
  • Text Blocks (Second Preview) – JEP 368
  • Packaging Tool (Incubator) – JEP 343
  • NUMA-Aware Memory Allocation for G1 – JEP 345
  • JFR Event Streaming – JEP 349
  • Non-Volatile Mapped Byte Buffers – JEP 352
  • ZGC on macOS – JEP 364
  • ZGC on Windows – JEP 365
  • Foreign-Memory Access API (Incubator) – JEP 370
1. Pattern Matching for instanceof (Preview) : Before Java 14, we use instanceof and cast to check the object’s type and cast to a variable. For example.

if (obj instanceof String) { // instanceof
      String str = (String) obj; // cast after type check
      if("test".equals(str)){
      }
} else {
     System.out.println("value is not a string");
}

Now using Java 14, we can refactor above code like this.if obj is an instance of String, then it is cast to String and assigned to the binding variable str.

if (obj instanceof String str) { // instanceof, cast and bind the variable
    if("test".equals(str)){
    }
} else {
    System.out.println("value is not a string");
}

2.  Switch Expressions (Standard) – JEP 361 : The switch expression was a preview feature in Java 12 and Java 13; now it is officially JDK standard feature, it means from Java 14 and onward, we can return value via switch expressions (we can use yield or label rules).

private int getSwitchValueViaYield(String mode) {
        int result = switch (mode) {
            case "Developer", "Tester":
                yield 10;
            case "Lead":
                yield 20;
            case "CTO", "CEO", "Director":
System.out.println("Supports multi line block!");
                yield 30;
            default:
                yield -1;
        };
        return result;
 }

3. Helpful NullPointerExceptions – JEP 358 : Improved the description of NullPointerExceptions by telling which variable was null. Add -XX:+ShowCodeDetailsInExceptionMessages option to enable this feature.

public class NullPointerExample {

    public static void main(String[] args) {
        String input = null;
        String result = showLowerCase(input); 
        System.out.println(result);
    }

    public static String showLowerCase(String str){
        return str.toLowerCase();
    }
}

Before JAVA 14 :

D:\Learning\JAVA 14\src>java com/blogspot/edgydebug/NullPointerExample.java
Exception in thread "main" java.lang.NullPointerException
        at com.blogspot.edgydebug.NullPointerExample.showLowerCase(NullPointerExample.java:12)

        at com.blogspot.edgydebug.NullPointerExample.main(NullPointerExample.java:7)



With JAVA 14 (-XX:+ShowCodeDetailsInExceptionMessages) :

D:\Learning\JAVA 14\src>java -XX:+ShowCodeDetailsInExceptionMessages com/blogspot/edgydebug/NullPointerExample.java
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.toLowerCase()" because "<parameter1>" is null
at com.blogspot.edgydebug.NullPointerExample.showLowerCase(NullPointerExample.java:12)
at com.blogspot.edgydebug.NullPointerExample.main(NullPointerExample.java:7)




4. Records (Preview) – JEP 359 : This feature is introduced to simplify the process of custom class creation. With a record you need to simply define a class in the following way.
Student.java :

package com.blogspot.edgydebug;
public record Student(String name, String id) {}

RecordExample.java :

package com.blogspot.edgydebug;

public class RecordExample {

     public static void main(String[] args) {
          Student student = new Student("John","12AWS34");
          System.out.println(student.name() + " "+student.id());
     }

}

D:\Learning\JAVA 14\src>javac --enable-preview --source 14 com\blogspot\edgydebug\Student.java
Note: com\blogspot\edgydebug\Student.java uses preview language features.
Note: Recompile with -Xlint:preview for details.

D:\Learning\JAVA 14\src>java --enable-preview --source 14 com\blogspot\edgydebug\RecordExample.java
Note: com\blogspot\edgydebug\RecordExample.java uses preview language features.
Note: Recompile with -Xlint:preview for details.
John 12AWS34

  • The Java compiler will generate a constructor, private final fields, accessors, equals/hashCode and toString methods automatically.
  • Points related to record class :
  • Records cannot be abstract
  • A record can neither extend a class nor it can be extended by another class. It’s a final class.
  • Records cannot extend any other class and cannot define instance fields inside the body. Instance fields must be defined in the state description only
  • The body of a record allows static fields and methods
  • Declared fields are private and final

5. Text Blocks (Second Preview) – JEP 368 : Text Blocks were introduced as a preview feature in Java 13. The text block helps easy creation of multiline string literals. It’s useful in easily creating HTML and JSON or SQL query strings.
For example :
package com.blogspot.edgydebug;
public class TextBlockExample {
      public static void main(String[] args) {
                String text = """
                                    <html>
                                   <body>\
                                   <p>Hello, '\s' World</p>\
                                  </body>
                                  </html>
                                  """;
               System.out.println(text);
   }
}


Output :
D:\Learning\JAVA 14\src>java --enable-preview --source 14 com\blogspot\edgydebug\TextBlockExample.java
Note: com\blogspot\edgydebug\TextBlockExample.java uses preview language features.
Note: Recompile with -Xlint:preview for details.
<html>
<body> <p>Hello, ' ' World</p> </body>
</html>

6. Packaging Tool (Incubator) – JEP 343 :

New jpackage tool to package a Java application into a platform-specific package.
Linux: deb and rpm
macOS: pkg and dmg
Windows: msi and exe (package the JAR file into an exe file on Windows)


7. NUMA-Aware Memory Allocation for G1 – JEP 345 : New NUMA-aware memory allocation mode, improves the G1 performance on large machines.Add +XX:+UseNUMA option to enable it.

8. JFR Event Streaming – JEP 349 : Improved the existing JFR (JDK Flight Recorder) to support event streaming, it means now we can stream the JFR events in real-time, without the need to dump the recorded events to disk and parse it manually.

9. Non-Volatile Mapped Byte Buffers – JEP 352 : Improved FileChannel API to create MappedByteBuffer that access to non-volatile memory (NVM)  a memory that can retrieve stored data even after having been power cycled. For example, this feature ensures that any changes which might still be in the cache are written back to memory.

10. ZGC on macOS – JEP 364 : Java 11 – JEP 333 introduced the Z Garbage Collector (ZGC) on Linux, and now it ports to macOS.

11. ZGC on Windows – JEP 365 : Java 11 – JEP 333 introduced the Z Garbage Collector (ZGC) on Linux, and now it ports to Windows version >= 1803.

12. Foreign-Memory Access API (Incubator) – JEP 370 : Introduce an API to allow Java programs to safely and efficiently access foreign memory outside of the Java heap.

No comments:

Post a Comment