In September 2021, Oracle launched version 17 of Java. For those who are counting: that is 6 new versions in less than 3 years. You almost start to wonder whether there is still news with so many releases in a row. We can be brief about that:Java Development Kit 17 has some nice new features. And you can also look into the future. Curious about what you can do with JDK 17? Let’s show you.
Faster development of Java Development Kits
The choice to look back over six versions does not come out of the blue. The beta of JDK was launched in 1995 and updated every few years. To speed up this development, Oracle has been releasing a new version every 6 months since 2018. The plan is to release an LTS (Long Term Support) release every two years. That is not quite running to schedule yet. The previous LTS was Java 11 in 2018, which is why we go back 6 versions. The one before that was version 8 (2014). Java 17 is planned to be followed by Java 21 as LTS version in 2023.
New features vs. preview features
So much for versioning. What changes for developers? Quite a lot. With the faster release schedule, Oracle has the freedom to test new features and let developers work with them before they are formally included in the version. Over the course of one or two releases you can already work with previews of these features. For example, JEP 359 Records was a preview in JDK 14 and 15 before it was turned on by default in JDK 16. You can enable preview features by adding -enable-preview as an argument to the compiler.
The three best features of Java Development Kit 17
Because Java 17 is an LTS release, it is likely that the majority of companies will upgrade to it. That makes an LTS more interesting than the interim updates. Three features in this version deserve extra mention. These are:
Local variable type inference;
Pattern matching with ‘instanceof’.
Local variable type inference
A bit of Java code quickly degenerates into a language mess. Local variable type inference helps to reduce this. In older versions of Java you have to declare the type of a variable twice. Assuming you use <> (added in version 7) you need one set of parameters, if they are identical on both sides of the equation. Then you have to repeat the same variable in the HashMap. In version 17, you name a keyword as a variable and let the compiler figure it out. That saves a few lines of code.
Two important details when declaring a variable
It sounds simple, but there are two things to bear in mind when declaring a variable. First of all: you cannot dynamically change the type of variable. A string remains a string. You can't spontaneously change it to an int or a float. Second, as soon as you call the variable, you have to give it a type. The compiler does not go through your code to find out what the variable will eventually do.
In previous versions of Java Development Kits, we have all been looking for that one missing break within a switch statement. Even a simple switch generates quite a few lines of code and each statement needs to end with a break. If you forget the break, the compiler executes the code on the next line as if it were part of the previous one and you spend an evening debugging. JDK 17 solves this problem by expanding our arsenal with switch expressions.
In a switch expression, break is not necessary to indicate that the line to be executed ends. Instead, you use -> to create lines. Left of the arrow you define the case, right the action to be performed. This saves a lot of lines of code and prevents unexpected results (if you forget to break). Also, within the switch expression you can choose between return or yield as operator. Return returns the value that belongs to the variable. With yield, you can first perform an operation on that value.
Pattern matching with ‘instanceof’
Pattern matching was a preview feature in Java 14 and Java 15. In Java 16 it was officially added. Because Java 17 is the LTS version, more developers now have access to this innovation. Instanceof has been used for a long time to check if a variable is of a certain type. In previous versions, you could then define other actions based on true or false. Pattern matching makes it possible to take action directly on the finding of the instanceof operator. This saves on the number of lines of code needed.
Determine type and execute action in one line
In the former application of instanceof you first declare that you are going to determine the type of a certain variable. Then, based on the result, you can retrieve the contents of that variable and do something with it. In the new version of instanceof you can state in one line that you want to determine the type and - within that same line - immediately indicate what you want to do with the content of a specific outcome. If the variable is of type string, and has as content 'dog', you can immediately give the command to show 'woof' on the screen. In older versions, this requires several lines of code.
Pattern matching for switch
Pattern matching for the switch operator is a preview feature in Java 17. This allows you to search within a switch statement with instanceof for a certain type of variable and take action based on that. For example, you can search through a binary tree with switch and use the return operator to return the contents of a specific node. You can already experiment with this feature in Java 17 by adding -enable-preview as an argument in the compiler.
Get going as a Java Developer
Java 17 contains many innovations. These are just a few examples. Are you a Java developer and do you want to work with the latest and greatest version of JDK? We are looking for professionals who can get the most out of this software.