Java 14 has finally been released in March 2020 and brings a whole host of new features that will help ease coding frustrations.
In this article, we will break down the top 6 features that make Java 14 an outstanding update compared to the previous versions. Here we go:
1. Switch Expressions
Although Switch Expressions was just a preview feature over the previous two versions, it has now been given permanent status in version 14.
The lambda syntax was introduced for switch expressions in Java 12 and this means that multiple case labels for pattern matching could be produced. This also stopped any fall-through that led to verbose code and enforced exhaustive cases that would issue a compilation error if all input cases weren’t entered.
In the previous versions yield statements were introduced to replace a break for returning values from an expression. Within Java 14, all these features are included as standard now. We should also point out that yield is not a new keyword for Java and is just used within switch expressions.
2. Previewing Text Blocks
Another former preview item, Text Blocks were first added into Java 13 with the intention to make multiline string literals much easier to create. In particular, HTML, JSON and SWL query strings became much easier as a result.
“Text blocks do remain a preview within Java 14, but they have some interesting new additions,” says Robert Class, a journalist at NextCoursework and Australia2Write. “In particular, you can now use a backslash to display smart multiline string blocks.”
\s can build trailing spaces that are ignored by the compiler as a default. This then preserves all the spaces that are included before it.
3. Pattern Matching for instanceof
Most codebase creations by Java developers will include a strong use of
instanceof conditions filtered in throughout the code. Generally speaking, the
instanceof conditional check normally comes before an explicit typecast.
Within the Java 14, developers will be happy to know that this has been removed to make conditional extraction a lot clearer. The scope of this variable is currently limited to just the conditional block.
4. Useful NullPointerExceptions
Most developers will tell you that the null pointer exceptions have been a complete nightmare in previous versions of Java. The infamous NPEs can be exceedingly difficult to debug.
This invariably led to developers falling back on other debugging tools or trying to manually figure the variable/method that was null because the stack trace shows only the line number.
“In Java 14, we also see the introduction of a brand new JVM feature, which has enhanced insights with a descriptive stack,” suggests Anita Lockfield, a tech writer at Britstudent and Write My X. “This is not a language feature, but a development of the runtime environment.”
5. Previewing Records
Within Java, you can build classes to hold data and utilize encapsulation to control the way that the data is accessed and modified. It is an object-oriented language.
Because it uses objects, this makes manipulating complicated data types easy and straightforward. This is one of the things that makes Java popular as a platform. The problem has been that the creation of data types has been verbose in old versions, and it needs a lot of code for even the simplest case.
Within Java 14, they have introduced records as a brand-new preview feature. This new concept helps developers to include a new language feature without having to make it part of the Java standard. This means that developers can test features and provide feedback that leads to changes before those features become standard.
If you want to use the preview feature, you need to specify the command line flag,
--enable-preview for the compilation and runtime. For compilation, you must also specify the
sourceflag. The record becomes a much simpler way of showing a data class.
6. New APIs
There are three new APIs within Java 14 that we love. The first is java.io which contains a new annotation type called Serial. This was designed to be used with compiler checks on Serializations. This means that annotations of this type can be applied to serialization related methods and with the fields in any classes declared to be Serializable.
The second API that we enjoyed is java.lang. This class has two distinct methods for the new Record feature, including
getRecordComponents(). These have a range of
RecordComponent objects and gives up to eleven ways to retrieve things, like the details of annotations and the generic type.
Finally, the new
java.util.concurrent.locks API has one new method called
setCurrentBlocker. This provides an ability to un-park and park a thread. This helps avoid the same problems that previous versions have had with the
Thread.resume() methods. You can also set the Object that will be returned by getBlocker with this new API, which can help when recalling the no-argument park method from a non-public object.
- What Makes Java 14 Special? - May 18, 2020