Java news: What are the best improvements brought by the updates for the new versions SE 8 – SE 17?

The Java programming language undergoes regular updates that bring various changes. Some are significant, others less so. The list of these can be quite long, so we’ve selected only the most important ones from each version.

The second most widely used programming language is Java

Java is one of the most popular programming languages. It is versatile, stable and 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 moving forward, which has made it necessary to move to an agile method of releasing a new version every 6 months.

The first agile version was Java SE 9, which was released in September 2017. Version 10 subsequently appeared after another six months. Other updates started to be released at a similar interval.

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

The most popular is Java 11

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

Also interesting were the answers to the question what are the reasons that people have not switched 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 changes because their current environment is working well.

Other reasons why survey participants stick with older versions include:

  • the high cost of migration,
  • customers’ reluctance to migrate,
  • the new version has no special improvements,
  • application servers or libraries do not support the latest versions.

The 2020 survey, on the other hand, found that the strategy of most developers (55%) is to stick to 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 newly introduced functions are important enough to justify migration.

The most important updates in each Java version

Each new version brings with it some changes. Some are larger, 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 three most important ones:

Modularity – Jigsaw Project

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

  • Shading,
  • reducing conflicts between versions,
  • adding encapsulation,
  • higher security,
  • higher performance,
  • customized runtime JDK with smaller size.

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

Private methods in Java interfaces

Another Java 9 feature allows writing private and static methods in interfaces to avoid redundant code.

Private and static methods code in Java 9
The Java 9 version includes the feature of writing 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 its name suggests, it is used to run commands, create variables, classes, methods and allows you to quickly test them. It’s great for testing small snippets of code that would otherwise require creating a new class using the main method.

JShell can be found in the <JAVA_HOME> / binfolder and you can run it by typing jshell in 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, for example, extended support for older versions of Java, you have to pay for the services.

Variable var

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

However, developers may wonder what the difference between var and Object is. The var keyword means that you want the compiler to detect the type itself. On the other hand, you use Object to convert your object instance to the Object type. This means that although you can use the general methods of the Object class, you cannot use its class-specific methods. This will create 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 major change in this version is that the developers does not need to explicitly compile the ava source files with the javac command.

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

Instead, it can execute the file directly using 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 has support for using local variable syntax in lambda parameters. The function 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 came with only one significant improvement. It was an alternative switch entry. The following example compares the old and new notation of switch. 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 directly assigned a value from a switch statement.

Java SE 13

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

Text blocks

The new text block feature made it easier for developers to read multi-line text. It uses three quotes, 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 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

In March 2020, Java SE 14 was released and brought up to four major enhancements:

Switch as standard

The switch expression, which was introduced as a preview in Java 12 and was improved in version 13, 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 hasn’t had much meaningful value in the past. The reason was that it could only detect that a certain value was zero in a given set. However, everything is different in the new version. The function has been extended and developers will finally know which variable caused the exception.

Java records

Records were introduced to reduce repetitive standard code in the POJO data models. They simplify day-to-day development, increase efficiency and significantly minimise the risk of human error. For example, a data model for a user with an ID and password can be simply 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

Version 14 introduced pattern matching 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. Using the keywords sealed, non-sealed and permits , the developer can 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 also specified that the only classes that can extend it are Employee and Manager. Extending a sealed class is done 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 by the compiler.

Hidden classes

The goal 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. Classes such as these usually have a short life cycle, so they are designed to be mainly efficient. They are especially 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 has been migrated from Mercurial to Git. The reason was the size of the metadata files, better tools and hosting.

Java SE 17

Until March 2022, the latest version of Java 17, which brought 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.

The best extensions for Java

Individual Java updates have many more new features. However, not all of them are as big and important as the ones we have selected. With them, you not only get cleaner, but also better code and faster and better implementation. If you’d like to see all the news, please take a look at 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