Core Java

Java 17 New Features Tutorial

In this article, we will present the new features of Java 17.

1. Introduction

JDK 17 is a long-term support (LTS) version and was released on September 14, 2021. It has the following new features:

Java 17
  • JEP 415: Context-Specific Deserialization Filters
  • JEP 414: Vector API (Second Incubator)
  • JEP 412: Foreign Function & Memory API (Incubator)
  • JEP 411: Deprecate the Security Manager for Removal
  • JEP 410: Remove the Experimental AOT and JIT Compiler
  • JEP 409: Sealed Classes
  • JEP 407: Remove RMI Activation
  • JEP 406: Pattern Matching for switch (Preview)
  • JEP 403: Strongly Encapsulate JDK Internals
  • JEP 398: Deprecate the Applet API for Removal
  • JEP 391: macOS/AArch64 Port
  • JEP 382: New macOS Rendering Pipeline
  • JEP 356: Enhanced Pseudo-Random Number Generators
  • JEP 306: Restore Always-Strict Floating-Point Semantics

In this tutorial, I will demonstrate JEP 406 – Pattern Matching for the switch – via a JShell command.

2. Technologies Used

The example code in this article was built and run using:

  • Java 17
  • Powershell
  • Docker

3. Install JDK17 as a Docker Container

In this step, I will install JDK 17 from the OpenJDK docker image.

3.1 Pull the JDK17

I will pull the latest openjdk image from the docker registry with the following command.

docker pull openjdk

PS C:\MaryZheng\DockerImages> docker pull openjdk
Using default tag: latest
latest: Pulling from library/openjdk
58c4eaffce77: Pull complete                                                                                             e6a22c806ee8: Pull complete                                                                                             e24190594061: Pull complete                                                                                             Digest: sha256:c841c22e8f9de75a637f9850952ea89a931bdb437af6c2d943ab337cdb299a5e
Status: Downloaded newer image for openjdk:latest
docker.io/library/openjdk:latest
PS C:\MaryZheng\DockerImages>

3.2 Run JDK 17 Docker Container

Enter the following command to start Java 17 and verify with the java version command.

docker run -it openjdk bash

PS C:\MaryZheng\DockerImages> docker run -it openjdk bash
bash-4.4# java -version
openjdk version "17.0.1" 2021-10-19
OpenJDK Runtime Environment (build 17.0.1+12-39)
OpenJDK 64-Bit Server VM (build 17.0.1+12-39, mixed mode, sharing)
bash-4.4#

Note: JDK 17.0.1 is installed.

4. Pattern Matching for Switch

I will demonstrate JDK 17 feature – Pattern Matching for switch – via Jshell commands. The switch statement is enhanced to match expressions with multiple patterns. Each pattern has a specific operation, so complex data patterns can be expressed concisely and safely.

4.1 Launch Jshell

Enter the following command to launch Jshell.

bash-4.4# jshell --enable-preview
Oct 23, 2021 12:38:51 PM java.util.prefs.FileSystemPreferences$1 run
INFO: Created user preferences directory.
|  Welcome to JShell -- Version 17.0.1
|  For an introduction type: /help intro

jshell>

4.2 Switch Statement

Enter the following Switch statement which matches four expressions – Integer, Long, Double, and String – in a Jshell prompt.

testSwitch method

String testSwitch(Object o){
String out="NA";
switch (o) {
        case Integer i -> out= String.format("int %d", i);
        case Long l    -> out= String.format("long %d", l);
        case Double d  -> out= String.format("double %f", d);
        case String s  -> out= String.format("String %s", s);
        default        -> out= o.toString();
    };
return out;
}

The JShell output confirms that the testSwitch method is created.

jshell> String testSwitch(Object o){
   ...>     String out="NA";
   ...>     switch (o) {
   ...>                 case Integer i -> out= String.format("int %d", i);
   ...>                 case Long l    -> out= String.format("long %d", l);
   ...>                 case Double d  -> out= String.format("double %f", d);
   ...>                 case String s  -> out= String.format("String %s", s);
   ...>                 default        -> out= o.toString();
   ...>         };
   ...>     return out;
   ...> }
|  created method testSwitch(Object)

jshell>

Verify the testSwitch method with four different object types.

verify testSwitch method

jshell> testSwitch(100)
$2 ==> "int 100"

jshell> testSwitch("Mary Test")
$3 ==> "String Mary Test"

jshell> testSwitch(11.11)
$4 ==> "double 11.110000"

jshell> testSwitch(100000l)
$5 ==> "long 100000"

jshell>

As you see here, different object types follow the same logic.

5. Summary

In this article, we demonstrated one of the JDK17 features – switch pattern matching – via the JShell command in a docker container. You can learn more about JDK 17 features here.

6. Download the Source Code

Download
You can download the full source code of this example here: Java 17 New Features Tutorial

Mary Zheng

Mary has graduated from Mechanical Engineering department at ShangHai JiaoTong University. She also holds a Master degree in Computer Science from Webster University. During her studies she has been involved with a large number of projects ranging from programming and software engineering. She works as a senior Software Engineer in the telecommunications sector where she acts as a leader and works with others to design, implement, and monitor the software solution.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button