Java News: What are the best improvements in the updates for the new versions SE 8 – SE 17?

The Java programming language is subject to regular updates that bring various changes. Some are significant, others less so. The list can be quite long, so we have selected only the most important ones from each version.

The second most popular programming language is Java

Java is one of the most widely used programming languages. It is versatile, stable, object-oriented and has a long history. It was first introduced in 1995. Oracle, the company that created it, initially released new major versions every 3-4 years. However, technology is constantly evolving, so it was necessary to move to an agile method of releasing a new version every 6 months.

The first agile version was Java SE 9, released in September 2017. Version 10 followed after a further six months. Other updates have been released at similar intervals.

New versions of the Java programming language from Oracle
Overview of the Java version, source: wikipedia.org

Most popular is Java 11

According to Snyk’s annual survey, Java 11 was the most used version in 2021. Up to 61.5% of respondents were using it. Only 12% had upgraded to the latest version, which at the time of the survey was Java 15.

Also interesting were the responses to the question of why people had not upgraded to a newer version. Most agreed that they were unable or unwilling to migrate Java every 6 months. Others, around 51%, say they do not need to change because their current environment works well.

Other reasons given by respondents for staying with older versions include:

  • the high cost of migration,
  • customers’ reluctance to migrate,
  • the new version does not have any specific improvements,
  • application servers or libraries do not support the latest versions.

In contrast, the 2020 survey found that the strategy of most developers (55%) is to stick with long-term releases. Despite this, up to 22% of respondents say they plan to decide whether or not to innovate. They want to see if the new features introduced are important enough to justify migration.

Top updates for each version of Java

Each new version brings with it some changes. Some are bigger, others smaller. We’ve picked out the key changes for you that could be useful to anyone who uses Java.

Java SE 9

Java SE 9 was released in September 2017 and brought with it several changes. But not all of them were significant and important. These are the top three:

Modularity – Project Jigsaw

The most significant change in the Java 9 environment was the modularity system, which was named the Project Jigsaw. A module is a group of code characterized by certain properties. The modularity feature breaks down a programming language into smaller models to:

  • provide shading,
  • reduce conflicts between versions,
  • add encapsulation,
  • provide higher security,
  • provide higher performance,
  • provide customized runtime JDK with smaller size.

To create a new model, you need the module-info.java file in your src.main.java directory. In it you can define your module requirements.

Private methods in Java interfaces

Another Java 9 feature is the ability to write private and static methods in interfaces to avoid redundant code.

Private and static methods code in Java 9
Java 9 includes the ability to write private and static methods in interfaces.

Java JShell command line tool

Oracle has developed a new REPL tool called JShell. REPL stands for Read – Evaluate – Print – Loop. As the name suggests, it is used to execute commands, create variables, classes and methods, and allows you to test them quickly. It’s great for testing small snippets of code that would otherwise require creating a new class with the main method.

JShell can be found in the <JAVA_HOME> / bin folder and you can run it by typing jshell at the command line and start writing code immediately.

JShell command line code in Java SE 9
The Java JShell command-line tool is great for testing small snippets of code.

Interactive Java jshell comes with history and autocomplete. It also provides features such as saving and loading from files and all or some lines of code:

Autocomplete and history of interactive Java JShell
A new REPL tool called JShell allows you to save and load from files and lines of code.

Java SE 10

Java SE 10 was released by Oracle in March 2018. It brought two important changes for developers:

OpenJDK

To make the Java community more Java friendly, Oracle has started promoting OpenJDK binaries as the primary JDK for the future. The problem is that if you want to get extended support for older versions of Java, for example, you have to pay for the services.

Var variable

The var variable is not new, as it exists in other programming languages. Its main advantage is that it reduces the verbosity of the language. In practice, this means using it to avoid long object names or typing the same name twice.

However, developers may wonder what the difference is between var and object. The var keyword means that you want the compiler to determine the type itself. On the other hand, you use Object to convert your object instance to the type Object. This means that although you can use the general methods of the Object class, you cannot use its class-specific methods. This will cause a compilation error.

Java SE 11

Java SE 11 was released six months after version number 10, in September 2018. It brought two major changes:

Running Java files

The main change in this version is that developers no longer need to explicitly compile the ava source files using the javac command.

Code to run Java files without compiling source files
In Java 11, you no longer need to explicitly compile ava source files using the javac command.

Instead, you can run the file directly with the java command.

Code - Direct launch of Java files in SE version 11.
Direct launch of Java files in SE version 11.

Using local variable syntax

Java 11 provides support for using local variable syntax in lambda parameters. This can be used to apply modifiers to local variables, for example to define an annotation.

Code - Using Local Variable Syntax in Java SE 11
Java SE 11 allows the use of local variable syntax in lambda parameters.

Java SE 12

Version 12 was released in March 2019 and had only one significant improvement. It was an alternative notation for switch. The following example compares the old and new switch notation. The code distinguishes between weekdays and weekends.

Code - old switch notation in Java SE 12
Java 12 improved the old switch expression notation.
Code - Alternative notation for switch in Java 12
Java SE 12 introduced a new alternative notation for the switch expression.

The code in the new notation looks much cleaner and shorter. The new switch form also removes the need for break keywords. This means that when the code finds the first match, it stops working. Another difference is that a variable can be assigned a value directly from a switch expression.

Java SE 13

Java 13 was released in September 2019. The update came with two major improvements:

Text blocks

The new text block feature makes it easier for developers to read multi-line text. It uses three quotation marks, similar to Python and Groovy.

Code - New text block feature in Java SE 13
Java 13 introduces a new text block feature that uses three quotes like Python and Groovy.

In addition, all the functions of the String class are available.

Code - Functions of class String in Java SE 13
In Java SE 13 you also have all the functions of the String class.

Enhancements to the switch expression

Java 13 enhances the switch expression from version 12 with the yield keyword. You can use yield to return values from a switch expression.

Code - Improved switch expression from version 12 in Java SE 13
Java SE 13 offers an improved switch expression from version 12.

Java SE 14

Java SE 14 was released in March 2020, bringing up to four major enhancements:

Switch as standard

Introduced as a preview in Java 12 and enhanced in version 13, the switch expression has become a language standard.

Code - Switch as a language standard in Java SE 14
Java SE 14 turned the original term switch into a language standard in programming.

NullPointerException improvement

NullPointerException didn’t have much use in the past. The reason was that it could only detect that a given value in a given set was zero. However, things are different in the new version. The function has been extended and developers will finally know which variable caused the exception.

Java records

Records have been introduced to reduce repetitive standard code in POJO data models. They simplify day-to-day development, increase efficiency and significantly reduce the risk of human error. For example, a data model for a user with an ID and password can simply be defined as

Code - New records feature in Java SE 14
Java 14 introduced POJO records to reduce repetitive standard code in data models.

In this case, the new record keyword was also used, which automatically adds constructor methods, getters, equals, hashCode and toString.

Pattern matching for instanceof

Pattern matching was introduced in version 14 to simplify the code. Without the new feature the code would look like this:

Code - Pattern matching using the instanceof function in Java 14
The old instanceof function notation for pattern matching was complex.

However, thanks to the new version of Java, the code looks much simpler and shorter:

Code - Improved instanceof function in Java 14
A new feature of Java 14 has simplified the writing of the instanceof expression.

Java SE 15

Java SE 15 was released in September 2020 and brought with it three major updates:

Text blocks as standard

Text blocks, which were introduced as a preview in the Java 13 environment, have become a language standard.

Sealed class

The public, protected and private access modifiers provide very coarse control. The keywords sealed, non-sealed and permits allow the developer to play with the inheritance hierarchy. The goal of sealed classes is to allow individual classes to declare which types can be used as subtypes. This also applies to interfaces and determining which types can implement them.

Code - Inheritance hierarchy using sealed classes
Sealed classes allow the declaration of types that can be defined as subtypes in each class.

In this example, we have declared an abstract class called Person. We have also specified that the only classes that can extend it are Employee and Manager. Extending a sealed class is done in the same way as in Java today, using the extends keyword:

Code - Sealed class extension in Java SE 15
Use extends to extend sealed classes in the current version of Java and in SE 15.

 

Any class that extends a sealed class must itself be declared sealed, non-sealed, or final. This ensures that the class hierarchy remains finite and is known to the compiler.

Hidden classes

The purpose of hidden classes is to allow you to create classes that are not visible. This means that they cannot be associated with other classes, nor can they be discovered by reflection. Such classes tend to have a short lifecycle, so they are primarily designed to be efficient. They are particularly useful for developers working with the JVM.

Java SE 16

Version 16 was released in March 2021 and brought one major change, which was the migration of OpenJDK to Git. The OpenJDK source code was migrated from Mercurial to Git. This was due to the size of the metadata files, better tools and hosting.

Java SE 17

Until March 2022, the latest version of Java 17, which brings a new rendering pipeline for MacOS systems, port support for Apple’s latest M1 chip, and support for sealed classes. There are also language improvements and the removal of ahead-of-time and just-in-time complications.

Best Java extensions

Individual Java updates have many more new features. But not all of them are as big and important as the ones we have selected. They will not only give you cleaner code, but also better code and faster and better implementation. If you’d like to see all the news, check out the following resources:

About the author

Jozef Wagner

Java Developer Senior

Viac ako 10 rokov programujem v Jave, momentálne pracujem v msg life Slovakia ako Java programátor senior a pomáham zákazníkom implementovať ich požiadavky do poistného softvéru Life Factory. Vo voľnom čase si rád oddýchnem v lese, prípadne si zahrám nejakú dobrú počítačovú hru.

Let us know about you