KEMBAR78
Java 14 features | PPTX
Java 14 Features
Aditi Anand
Software Developer
Highlights
 Java 14 features we are going to walk through in this presentation are:
 Switch Expressions
 Pattern matching for instanceof
 Helpful NullPointerException
 Records
 Text Blocks
Switch Expressions
 For switch expressions, lambda expressions incorporating no fall-through (break
not required anymore after each case) and comma separated multiple labels
introduced in Java 12 along with yield with a returning statement in Java 13 as
preview, holds a permanent status from Java 14 onwards.
var log = switch (event) {
case PLAY -> "User has triggered the play button";
case STOP, PAUSE -> "User needs a break";
default -> {
String message = event.toString();
LocalDateTime now = LocalDateTime.now();
yield "Unknown event " + message +
" logged on " + now;
}
};
Pattern matching for instanceof (Preview)
 Preview feature introduced which eliminates the requirement of explicit cast
done after the conditional instanceof checks to operate on that object.
 Before Java 14:
if (obj instanceof Group) {
Group group = (Group) obj;
// use group specific methods
var entries = group.getEntries();
}
 After Java 14:
if (obj instanceof Group group) {
var entries = group.getEntries();
}
Helpful NullPointerExceptions
 NullPointerExpection involves a lot of debugging to find the root cause when
we look at the application logs. To ease this problem, Java 14 has introduced
more informative diagnostics with the exception thrown.
 If we are getting location as null from below code:
var name = user.getLocation().getCity().getName();
 The NPE thrown from Java 14 onwards is:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke
"Location.getCity()" because the return value of "User.getLocation()" is null
at NullPointerExample.main(NullPointerExample.java:5)
Records (Preview)
 This preview feature make the code concise wherein if the intent of the class is
clear then on declaring the record as below we don not have to worry about
writing various components of the class like the constructor, getter methods,
toString(), hashCode() and equals() method.
Some additional facts:
 Fields of the record class are implicitly final.
 The constructor generated in the class is called canonical constructor.
 On compilation of a record, we will get a new class, java.lang.Record acting as a
supertype for the record class.
 The java.lang.Record class cannot be directly extended.
 We can use public API and canonical constructor to serialize and deserialize records.
 The serialVersionUID of a record class is 0L unless it is explicitly declared.
 The requirement for matching serialVersionUID values is also waived for record
classes.
Records (cont.)
Below is a brief code snippet to understand the concept better where we have the syntax to declare
records:
public record FXOrder(int units, CurrencyPair pair, Side side,
double price, LocalDateTime sentAt, int ttl) {}
On compiling with a preview flag: javac --enable-preview -source 14 FXOrder.java
we will be able to examine the class file with javap (showing only methods and their signatures below)
$ javap FXOrder.class
Compiled from "FXOrder.java"
public final class FXOrder extends java.lang.Record {
public FXOrder(int, CurrencyPair, Side, double, java.time.LocalDateTime, int);
public java.lang.String toString();
public final int hashCode(); public final boolean equals(java.lang.Object);
public int units(); public CurrencyPair pair(); public Side side(); public double price();
public java.time.LocalDateTime sentAt(); public int ttl();
}
Text Blocks (Second Preview)
 Text blocks introduced in Java 13 as preview remain there in Java 14 with a couple
of tweaks:
 s escape sequence, adding a single space
String text2 = """
line1
line2 s
line3
""";
  backslash, suppressing the new line character at the end of the line as per Java 13
preview of text block
String text = """
Lorem ipsum dolor sit amet, consectetur adipiscing 
elit, sed do eiusmod tempor incididunt ut labore 
et dolore magna aliqua.
""";
References
 Java 14 Features by Anupam Chugh https://www.journaldev.com/37273/java-
14-features
 Java 14 Arrives with a Host of New Features by Raoul-Gabriel Urma
https://blogs.oracle.com/javamagazine/java-14-arrives-with-a-host-of-new-
features
 New switch Expressions in Java 12 by Raoul-Gabriel Urma and Richard
Warburton https://blogs.oracle.com/javamagazine/new-switch-expressions-
in-java-12
 Inside Java 13’s switch Expressions and Reimplemented Socket API
https://blogs.oracle.com/javamagazine/inside-java-13s-switch-expressions-
and-reimplemented-socket-api
 Records Come to Java https://blogs.oracle.com/javamagazine/records-come-
to-java
Thank You!

Java 14 features

  • 1.
    Java 14 Features AditiAnand Software Developer
  • 2.
    Highlights  Java 14features we are going to walk through in this presentation are:  Switch Expressions  Pattern matching for instanceof  Helpful NullPointerException  Records  Text Blocks
  • 3.
    Switch Expressions  Forswitch expressions, lambda expressions incorporating no fall-through (break not required anymore after each case) and comma separated multiple labels introduced in Java 12 along with yield with a returning statement in Java 13 as preview, holds a permanent status from Java 14 onwards. var log = switch (event) { case PLAY -> "User has triggered the play button"; case STOP, PAUSE -> "User needs a break"; default -> { String message = event.toString(); LocalDateTime now = LocalDateTime.now(); yield "Unknown event " + message + " logged on " + now; } };
  • 4.
    Pattern matching forinstanceof (Preview)  Preview feature introduced which eliminates the requirement of explicit cast done after the conditional instanceof checks to operate on that object.  Before Java 14: if (obj instanceof Group) { Group group = (Group) obj; // use group specific methods var entries = group.getEntries(); }  After Java 14: if (obj instanceof Group group) { var entries = group.getEntries(); }
  • 5.
    Helpful NullPointerExceptions  NullPointerExpectioninvolves a lot of debugging to find the root cause when we look at the application logs. To ease this problem, Java 14 has introduced more informative diagnostics with the exception thrown.  If we are getting location as null from below code: var name = user.getLocation().getCity().getName();  The NPE thrown from Java 14 onwards is: Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Location.getCity()" because the return value of "User.getLocation()" is null at NullPointerExample.main(NullPointerExample.java:5)
  • 6.
    Records (Preview)  Thispreview feature make the code concise wherein if the intent of the class is clear then on declaring the record as below we don not have to worry about writing various components of the class like the constructor, getter methods, toString(), hashCode() and equals() method. Some additional facts:  Fields of the record class are implicitly final.  The constructor generated in the class is called canonical constructor.  On compilation of a record, we will get a new class, java.lang.Record acting as a supertype for the record class.  The java.lang.Record class cannot be directly extended.  We can use public API and canonical constructor to serialize and deserialize records.  The serialVersionUID of a record class is 0L unless it is explicitly declared.  The requirement for matching serialVersionUID values is also waived for record classes.
  • 7.
    Records (cont.) Below isa brief code snippet to understand the concept better where we have the syntax to declare records: public record FXOrder(int units, CurrencyPair pair, Side side, double price, LocalDateTime sentAt, int ttl) {} On compiling with a preview flag: javac --enable-preview -source 14 FXOrder.java we will be able to examine the class file with javap (showing only methods and their signatures below) $ javap FXOrder.class Compiled from "FXOrder.java" public final class FXOrder extends java.lang.Record { public FXOrder(int, CurrencyPair, Side, double, java.time.LocalDateTime, int); public java.lang.String toString(); public final int hashCode(); public final boolean equals(java.lang.Object); public int units(); public CurrencyPair pair(); public Side side(); public double price(); public java.time.LocalDateTime sentAt(); public int ttl(); }
  • 8.
    Text Blocks (SecondPreview)  Text blocks introduced in Java 13 as preview remain there in Java 14 with a couple of tweaks:  s escape sequence, adding a single space String text2 = """ line1 line2 s line3 """;  backslash, suppressing the new line character at the end of the line as per Java 13 preview of text block String text = """ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. """;
  • 9.
    References  Java 14Features by Anupam Chugh https://www.journaldev.com/37273/java- 14-features  Java 14 Arrives with a Host of New Features by Raoul-Gabriel Urma https://blogs.oracle.com/javamagazine/java-14-arrives-with-a-host-of-new- features  New switch Expressions in Java 12 by Raoul-Gabriel Urma and Richard Warburton https://blogs.oracle.com/javamagazine/new-switch-expressions- in-java-12  Inside Java 13’s switch Expressions and Reimplemented Socket API https://blogs.oracle.com/javamagazine/inside-java-13s-switch-expressions- and-reimplemented-socket-api  Records Come to Java https://blogs.oracle.com/javamagazine/records-come- to-java
  • 10.