Andersen

Pattern Matching and other useful features, as well as feedback from an expert

May 22, 2020
Blog

In the spring of 2020, Oracle released Java14 – a new version of Java. Oracle has recently started releasing new versions more often lately: now, updates come out at around six-monthly intervals. Does this mean that each new version won’t have many important changes? Does it make sense to upgrade your earlier-version platform to Java14?

We will try to highlight the key features added in Java14 and determine their real value. Artyom Pugachev, a Java Developer from Andersen, will help us with this.

Pattern Matching for instanceof

In Java14, the instanceof operator allows not only to check whether an object is a subtype of a given type but also to automatically cast it to this type, if possible. In previous versions, this operation required several lines, and now, there is the opportunity to make the code more compact. 

It is also possible to use this object further in the body of the condition, which allows avoiding additional if statements and simplifies the perception of the code.

NullPointerExceptions

In the new version, the message about NullPointerExceptions has become more informative. Now, in the case of NullPointerExceptions, the JVM not only reports the file name and line number where the needed object is missing but also indicates which variable has not been defined.

This error is not informative by default unless the option -XX:+ShowCodeDetailsInExceptionMessages for the virtual machine is activated. The appearance of this error on a public server can lead to a leak of information about the project structure. For security reasons, it is better to activate this option only on private test servers.

Switch Expressions

The switch operator, the functionality of which was previously announced in JDK 12 and JDK 13, has been elaborated for better perception and writing. The first difference: the writing format shows when it is neсessary to specify multiple options for the value of a variable, which corresponds to the common block of code. In the old version, it was necessary to write each value in a new line, indicating the case operator at the beginning of each value. In the 14th version, it is possible to specify multiple values within one case operator, dividing them with commas.

Also, the switch has become ‘switch expressions’, since now it is written with the arrow (->) instead of a colon and can return a value. The description of executable blocks of code has become similar to lambda expressions. If a block consists of more than one operator, it must be placed in curly brackets; if there is only one operator, the brackets are optional.

Records

Java14 introduced the ability to declare classes containing shallowly immutable data using Records. The syntax has become much more compact: now it is possible to write in one line what used to be written in 5-10 lines. 

Also, a new operator ‘records’ has been introduced on a temporary basis in order to get feedback on real use. When programming in Java, DTO classes that convey only the state of the object are often used. For such classes, one has to define constructors, as well as data access methods, equals (), hashCode (), and toString (). The new version provides the opportunity to use records to avoid creating duplicate code. The result of compiling the record will be a finalized class, with final fields, as well as automatically generated main methods. 

The main limitations are:

  • There is no ability to extend other classes, but there’s a possibility to implement an interface.
  • There is no possibility to define additional fields in the class body. They can be defined only in the description that is indicated in parentheses after the class name.
  • These components are final by default, and there is no way to extend from them.

Before

After

Text Blocks

Before the 14th version of Java, long text lines had to be split into several lines so that they were easy to read and did not go beyond the permissible line length, using additional concatenation characters. In the new version, the concept of “text block” has appeared. It opens and closes with three double quotation marks.

This text block can occupy any number of lines and will be perceived as a single element. Also, a special thing about this block is that all line breaks will be saved (displayed) upon output.

To cancel the transfer of the cursor to a new line, it’s necessary to use the backslash character ().

Artyom Pugachev also shared his feedback about the 14th version:

“Some developers may have really needed the features that are added in Java14. If you’re just planning to switch to Java, you definitely need to start with the latest version. But if you’ve already run the application on previous versions of Java and now just support it, the new features do not make worth it switching to Java14 and encountering related problems.”

Previous articleNext article