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

Java 16 is Here! New Features of Java 16 Simplified

by Imran Shaikh
Published: Last Updated on 3009 views
Java 16 is Here and Key Takeaways for Java Developers

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 improvement, which I personally 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. Meaning, 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 the Java 17 with LTS support.

Since releasing Java 9 to Java 16, Java has added a lot of features and improvements for which the 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 really makes a Java Developer feel happy, as we were tired of hearing “Java is Dead“.

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

You can follow me on social @coderstea on TwitterLinkedinFacebook, or Instagram. We also share high-quality videos about programming on our Youtube channel. You can also publish your own post on CodersTea.com, just share your thought on Contact Us.

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 finalized in this latest release of Java 16.

GraalVM Enterprise is added to 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 the Pattern Matching for instanceof and Records. If you are more into JEP, then its 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) { }

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

As you can see, we are checking the 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

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

Prior to Java 16, The restriction of this pattern matching was that the 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 to which will be used for packaging self-contained java applications.

This tool is based on the legacy JavaFX javapackager 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 application for native platforms instead of creating a jar and ask the user to setup 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 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 in the next release. There are 4 in 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 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 { ... }

This provide more declarative approach than the access modifier to restrict the use of super classes.

Conlusion 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 it, 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 of the organization nowadays, please check this post.

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

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

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

Privacy & Cookies Policy