Home Java Java 16 is Here! New Features of Java 16 Simplified

Java 16 is Here! New Features of Java 16 Simplified

Published: Last Updated on 0 comment

Hey, Tea Lovers! Today I will briefly talk about the latest release of Java 16 on March 16, 2021. I will talk about the key takeaway from the features and improvements, which I think will affect the day-to-day life of a Java developer. This post is heavily based on the press release “Oracle Announces Java 16”. If you want to see it in more detail, you can check out the press release here.

Before We Talk About Java 16

As you must have known that Java has adopted a 6-month release cycle. This means it will release a new version of Java every 6 months. And after every 3 years, an LTS version will be released. Currently, Java 11 is the LTS version. After 6 Months, around September, we will see Java 17 with LTS support.

Since releasing Java 9 to Java 16, Java has added a lot of features and improvements for that Java developers were longing for. And looking at it, we can say that Java is slowly and steadily catching up to the other programming languages. It makes a Java Developer feel happy, as we were tired of hearing “Java is Dead“.

As an LTS version, many companies are using Java 11 now. To know more about how Java 11 upgrades from the previous Java version, see my post The Things I Love and Use from Java 11.

I would be happy to connect with you guys on social media. It’s @coderstea on TwitterLinkedinFacebook, Instagram, and YouTube.

Please Subscribe to the newsletter to know about the latest posts from CodersTea.

Oracle Announces Java 16, “Oracle”

When they say Oracle announces Java 16, it means it is a licensed version from Oracle and is yet to be available for open-source vendors. So unless you have an Oracle license you can’t deploy it to your production environment, but you can use it for development purposes. So wait for a little time for an open-source release and to use it freely on production.

You can download the latest Java 16 or JDK 16 here.

TLDR; of Oracle Java 16 Press Release

Java 16 came with 17 enhancements (feature + improvement). These enhancements include tools, language improvements, memory management, and the next release’s incubating & preview features.

Pattern Matching and Records are finalized in this latest release of Java 16.

GraalVM Enterprise is added to the Java SE subscription, and Oracle says, “At no additional cost”

To know more about each feature in detail, click here.

It Began in Java 14

Two enhancements that Java 16 did to the feature introduced in Java 14 are Pattern Matching for instanceof and Records. If you are more into JEP, then it’s JEP 394 and JEP 395 respectively.

Record in Java 16

Record classes are similar to Data Classes from Kotlin. It reduces the pain of writing redundant POJO functions like getters, setters, toString, and so on. It was introduced in Java 14.

record Employee (int id, String name, String address) { }Code language: JavaScript (javascript)

The restriction on Record was the inner class of Record couldn’t declare an explicit or implicit static member. But starting Java 16, the restriction has been lifted and the static members of the inner class are now permitted. Finally, they can meet, what a happy ending.

Pattern Matching of instanceOf

Pattern matching of instanceOf improves the instanceOf condition and reduces the redundant code. For example, let’s look at the following.

if (obj instanceof String) {
     String s = (String) obj; 
     // do something with s
 }Code language: JavaScript (javascript)

As you can see, we are checking if obj is instanceOf String and after verifying it is true, we need to cast it to String. But Java already knows that it’s of String type so why cast it again? The solution is the Pattern Matching feature. Now the same code with pattern matching is as follows.

if (obj instanceof String s) {
     // do something with s
 }Code language: JavaScript (javascript)

Notice the difference? The second line is reduced to none and we have s in the if the condition itself. The explanation for this ‘pattern matching of instanceOf‘ is that if obj is instaceOf String then cast it to String and store it in variable s.

Before Java 16, The restriction of this pattern matching was that they s must be final (implicitly). But that restraint is removed from Java 16. The expression error is also moved to compile-time.

The new Packaging Tool for Java 16

Sitting in the incubator since the last two releases, Java has finally moved the packaging tool jpackage to the production-ready list. The JEP 392, provided the package which will be used for packaging self-contained java applications.

This tool is based on the legacy JavaFX javapackager the tool which was used for native packaging format. When we say native packaging, it means an installation file. Such as, in Windows, double click to install, or in MacOs, drag to install. With this, the Developer can make an installation package of java applications for native platforms instead of creating a jar and asking the user to set up the Java, path, and other things. With this, the user can have a natural installation process like other programs with native Java applications.

A Glimpse of the Next Feature in Java 16

The preview of the next feature or incubator is the package that contains the features or improvements to test and can be incorporated into the next release. There are 4 total incubating and preview features, namely  Vector API,  Foreign Linker API,  Foreign-Memory Access API, and  Sealed Classes.

You are not allowed, Unless…

If You are trying to access a class or interface, extend to implement, but you are unable to do so. The reason? You need permission from the class or interface because they didn’t like your code. Just kidding, it’s because they are a sealed class.

The sealed class allows one to describe which class or interface can extend or implement this class or interface. In short, we can put restrictions on who can use this class or interface. Suppose we have Student abstract class and if we make it a sealed class and permit only ScienceStudent and CommerceStudent to extend the class, then only those classes will be able to extend the Student and other classes will get an error if they try to extend it.

public abstract sealed class Student permits ScienceStudent , CommerceStudent { ... }Code language: PHP (php)

This provides a more declarative approach than the access modifier to restrict the use of superclasses.

Conclusion and Other changes in Java 16

As I said, there is a total of 17 enhancements in Java 16, but we only talked about the 4 changes. Because I think it is something that we developers will see in day-to-day life. But it doesn’t mean all other changes are not important. In my view, memory-based enhancements are the best and most effective changes in any version. Because they make our application much more faster and stable. If you want to see them in detail, please click here. This OpenJDK page has a list of all the enhancements provided in Java 16. I would have put them here, but I would have only copy-pasted them, so better to read on the official page instead.

Hope you liked the post. And if you want to learn the new feature of Java 11, an LTS version, which is used by most organizations nowadays, please check this post.

Please comment with your thoughts and let me know about them.

See you in the next post. HAKUNA MATATA !!!

I would be happy to connect with you guys on social media. It’s @coderstea on TwitterLinkedinFacebook, Instagram, and YouTube.

Please Subscribe to the newsletter to know about the latest posts from CodersTea.

Notify of
Inline Feedbacks
View all comments

@2023 All Right Reserved. Designed and Developed by CodersTea

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More