SlideShare a Scribd company logo
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!

More Related Content

PPTX
The Java memory model made easy
PPT
PPTX
모던자바의 역습
PDF
Complete Java Course
PPTX
Java byte code in practice
PPTX
Making Java more dynamic: runtime code generation for the JVM
PPTX
Java 10, Java 11 and beyond
PPTX
Unit testing concurrent code
The Java memory model made easy
모던자바의 역습
Complete Java Course
Java byte code in practice
Making Java more dynamic: runtime code generation for the JVM
Java 10, Java 11 and beyond
Unit testing concurrent code

What's hot (20)

PDF
Java Programming - 03 java control flow
PDF
Kotlin advanced - language reference for android developers
PPT
Invoke dynamics
PPTX
The definitive guide to java agents
PDF
Developing android apps with java 8
PPTX
Java 8 Feature Preview
PDF
Java Fundamentals
PDF
Kotlin Developer Starter in Android projects
PPTX
Use of Apache Commons and Utilities
PPT
Java Serialization
PPTX
PDF
Kotlin으로 안드로이드 개발하기
PPT
Java Basics
PPTX
Java For Automation
PDF
Moose
PPTX
Java and OpenJDK: disecting the ecosystem
PPTX
A topology of memory leaks on the JVM
PPTX
Code generation for alternative languages
PDF
Java Programming - 04 object oriented in java
PDF
Java Serialization Deep Dive
Java Programming - 03 java control flow
Kotlin advanced - language reference for android developers
Invoke dynamics
The definitive guide to java agents
Developing android apps with java 8
Java 8 Feature Preview
Java Fundamentals
Kotlin Developer Starter in Android projects
Use of Apache Commons and Utilities
Java Serialization
Kotlin으로 안드로이드 개발하기
Java Basics
Java For Automation
Moose
Java and OpenJDK: disecting the ecosystem
A topology of memory leaks on the JVM
Code generation for alternative languages
Java Programming - 04 object oriented in java
Java Serialization Deep Dive
Ad

Similar to Java 14 features (20)

PDF
JDKs 10 to 14 (and beyond)
PPT
Java Performance Tuning
PPTX
Java 9 features
PDF
Kotlin Advanced - language reference for Android developers
PPTX
Core Java Tutorials by Mahika Tutorials
PPTX
DevNexus 2020: Discover Modern Java
ODP
From Java 6 to Java 7 reference
PDF
Part 2-Support Java 17 Certif Pr Youssfi V2.pdf
PDF
JAVA UNIT-3 ONE SHOT NOTES_64415856_2025_07_12_10__250712_103718.pdf
PDF
JAVA UNIT-3 ONE SHOT NOTES_64415856_2025_07_12_10__250712_103718.pdf
PPTX
Pure Java RAD and Scaffolding Tools Race
PPT
Java 7 Language Enhancement
PPT
Mastering Java ByteCode
PPT
Scala uma poderosa linguagem para a jvm
PDF
New Features Of JDK 7
PPTX
interface in java explained in detailed form
PPTX
Java 7 & 8 New Features
PDF
FP in Java - Project Lambda and beyond
PPTX
Jug trojmiasto 2014.04.24 tricky stuff in java grammar and javac
PDF
JDKs 10 to 14 (and beyond)
Java Performance Tuning
Java 9 features
Kotlin Advanced - language reference for Android developers
Core Java Tutorials by Mahika Tutorials
DevNexus 2020: Discover Modern Java
From Java 6 to Java 7 reference
Part 2-Support Java 17 Certif Pr Youssfi V2.pdf
JAVA UNIT-3 ONE SHOT NOTES_64415856_2025_07_12_10__250712_103718.pdf
JAVA UNIT-3 ONE SHOT NOTES_64415856_2025_07_12_10__250712_103718.pdf
Pure Java RAD and Scaffolding Tools Race
Java 7 Language Enhancement
Mastering Java ByteCode
Scala uma poderosa linguagem para a jvm
New Features Of JDK 7
interface in java explained in detailed form
Java 7 & 8 New Features
FP in Java - Project Lambda and beyond
Jug trojmiasto 2014.04.24 tricky stuff in java grammar and javac
Ad

Recently uploaded (20)

PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
history of c programming in notes for students .pptx
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
AI in Product Development-omnex systems
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Nekopoi APK 2025 free lastest update
PDF
System and Network Administration Chapter 2
PPTX
ai tools demonstartion for schools and inter college
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Design an Analysis of Algorithms I-SECS-1021-03
Softaken Excel to vCard Converter Software.pdf
Navsoft: AI-Powered Business Solutions & Custom Software Development
history of c programming in notes for students .pptx
Wondershare Filmora 15 Crack With Activation Key [2025
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
AI in Product Development-omnex systems
Odoo POS Development Services by CandidRoot Solutions
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Upgrade and Innovation Strategies for SAP ERP Customers
Operating system designcfffgfgggggggvggggggggg
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Nekopoi APK 2025 free lastest update
System and Network Administration Chapter 2
ai tools demonstartion for schools and inter college
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
VVF-Customer-Presentation2025-Ver1.9.pptx
How Creative Agencies Leverage Project Management Software.pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...

Java 14 features

  • 1. Java 14 Features Aditi Anand Software Developer
  • 2. 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
  • 3. 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; } };
  • 4. 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(); }
  • 5. 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)
  • 6. 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.
  • 7. 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(); }
  • 8. 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. """;
  • 9. 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