Java 16 features every Java developer should know

Photo by Jan Kopu0159iva on Pexels.com

Java 16 just released.

Though a lot of new features were introduced only six of them need to be understood by day to day java developers.

Here are those features:

  1. Records
  2. Pattern matching for instanceof
  3. Sealed classes (Second preview)
  4. jpackage packaging tool
  5. Stream.toList() method
  6. Day Period support added to java.time Formats

1.Records:

If you want to create a Java Bean class just to act as data carriers you need to write a lot of code:

  • Declare object attributes
  • Create Getters and Setters
  • Override equals() , hashCode() and toString() methods
  • Create constructors if required

All those can be thrown away using Records! This saves developer time and improves the elegance of code.

Here is a detailed explanation of Records

2. Pattern matching for instanceof

If you want to check the class type of an object type and then perform some operation on it based on the type , you need to first use instanceof operator to check the instance type , then cast it to the required object type before performing any required operation.

The casting operation can be ignored using pattern matching introduced in Java 16.

Here is a detailed explanation of it.

3. Sealed Classes

Can you ever dictate what classes can inherit the class you created?

You can do so using Sealed classes in java.

Here is a detailed explanation of it.

This feature is not a permanent feature of Java yet (It is in second preview) and most likely to become a permanent feature in the next version release.

4.Packaging Tool

Did you ever want to create a native application in Java?

An application which can be installed in your Windows or Unix machine .

You just click the exe file (in Windows) and it takes you through the steps to install it as a native application.

You then open the app from Start Menu.

Java has provided a packaging tool named jpackage which does exactly that. It was in incubator mode and now has become a permanent feature in the current version release of Java 16.

Here is a demo of how to create a “Hello World” native windows application using jpackage.

5. Stream.toList method

How do you convert a stream of objects to a list?

Until Java 16 you did it through Stream.collect() method and passed Collectors.toList() method as a parameter to the collect() method :

Stream.collect(Collectors.toList())

Starting Java 16 you can do it through an even more simpler Stream.toList() method .

Also the list you get out of Stream.toList() method is immutable whereas the list you get out of Stream.collect() method is mutable.

Here is a detailed explanation with example.

6.Day Period support

Can you print out what period of the day it is using Java?

Like if it is in the morning or in the evening or at night.

You can, starting Java 16.

You just need to use the letter “B” in your date time formatter pattern.

For example the below code prints the data and time along with the period:

String theTimeNow = DateTimeFormatter.ofPattern("h m B").format(LocalTime.now());

I tested this at 10:19 pm and it printed the following output:

10 19 at night

The pattern codes are as follows:

h for hours

m for minutes

B for period

The entire set of features can be seen in Java 16 release notes.

That’s it!


Posted

in

,

by

Tags:

Comments

Leave a Reply

Discover more from The Full Stack Developer

Subscribe now to keep reading and get access to the full archive.

Continue reading