This article introduces you to the new features of Java14:

  1. Instanceof pattern matching
  2. The text block is again retained as a preview feature
  3. Introduction of the Record Type
  4. The packing kit is finally here
  5. A composite garbage collector has been deprecated
  6. conclusion

With the arrival of the new Java release life cycle, which is expected in March 2020, this article provides an overview of five key features.

Java 13 has just been released to developers, and the latest version of the JDK was released in September 2019. But few companies are now switching to Java 13, as this version seems unlikely to get long-term support (LTS) from Oracle anyway. Not to mention now that Java 14 is here again.

With the arrival of the new Java release life cycle, a new Java release is expected in March 2020. Time is running out, which is why Mark Reinhold, Oracle’s Lead Java architect, has just suggested that five major features from the JDK Enhancement Proposal (JEP) should be included in Java 14.

Therefore, in this article I’ll give you an overview of these five main features. These features should be made part of Java 14 so that they can be made available to developers from March 2020.

1. Instanceof pattern matching

This new feature, provided as preview mode, is intended to improve Java by providing users with pattern matching for the Instanceof operator.

Pattern matching has emerged in other languages, making it possible to express program logic in a safer and more concise way.

The pattern matching of the Instanceof operator will help conditionally extract components from objects.

In most Java programs, there is code of this type:

1if (obj instanceof Integer) {

2    int intValue = (Integer) obj;

3    // ... use intValue ...

4}

Copy the code

The above code is not neat, nor is it very clear. In addition, repeating this type of construct in a program increases the risk of error.

The pattern matching of the Instanceof operator, introduced as a preview state in Java 14, will allow the following simplification of the above code:

1if (x instanceof Integer i) {

2    // ... use i as an Integer directly ...

3}

Copy the code

 1String formatted = "unknown";if (obj instanceof Integer i) {

2    formatted = String.format("int %d", i);

3}

4else if (obj instanceof Byte b) {

5    formatted = String.format("byte %d", b);

6}

7else if (obj instanceof Long l) {

8    formatted = String.format("long %d", l);

9}

10else if (obj instanceof Double d) {

11Formatted = the String. Format (" double % f", d);

12}

13else if (obj instanceof String s) {

14    formatted = String.format("
String %s", s);

15} / /... use formatted variable ...

16


Copy the code

In future Java 15, 16, or 17, we can imagine it is possible to replace the previous if/else sequence with the following code:

 1String formatted =

2    switch (obj) {

3        case Integer i -> String.format("int %d", i);

4        case Byte b -> String.format("byte %d", b);

5        case Long l -> String.format("long %d", l);

6        case Double d -> String.format("double %f", d);

7        case String s -> String.format("String %s, s);

8        default -> String.format("
Object %s", obj);

9}; / /... use formatted variable

10


Copy the code

2. Text blocks are again reserved as preview features

Text blocks were introduced as a preview feature in Java 13, and are retained again as a preview feature in Java 14.

In response to feedback collected from the major Java community following the release of Java 13, two new escape sequences have been added to text blocks.

The escape sequence character “\” explicitly eliminates the need to insert new line characters. Take the following example of splitting a larger string using the concatenation operator “+” between smaller strings:

1String literal = "This is a string splitted " +

2                 "in several smaller " +

3                 "strings.";

Copy the code

1String text = "" "

2                This is a string splitted \

3                in several smaller \

4                strings.\

5"" "
;

Copy the code

On the other hand, the new escape sequence character “\s” can be converted to a simple blank. This prevents whitespace characters from being cleared.

Based on this escape sequence, we can construct a string that ensures that each line is the same length:

1String colors = "" "

2red \s

3green\s

4blue \s

5"" "
;

Copy the code

3. The introduction of Record Type

Java 14 should see Record types introduced as a preview feature. Record objects allow a compact syntax to declare classes that are transparent holders of shallow-immutable data.

Like enumerated types, records are a restricted form of classes. A record declares its representation and submits it to the API corresponding to this representation. Record objects give up the freedom that Java classes benefit from: the ability to separate the API from its presentation. In return, record objects provide significant benefits in terms of brevity.

A record object has a name and a state description that declares its components. The body of the record object is optional. Here is an example of creating a record object Point:

1record Point(int x, int y) {}

Copy the code

 1final class Point {

2    public final int x;

3    public final int y;

4

5

6    public Point(int x, int y) {

7        this.x = x;

8        this.y = y;

9    }

10

11

12    // state-based implementations of equals, hashCode, toString

13    // nothing else

14}

Copy the code

1RecordComponent[] getRecordComponents(a)

2boolean isRecord(a)

Copy the code

4. Packing tools finally arrived

The packaging tool JPackage, which was removed from the periphery at the last minute before the Java 13 release, is finally available in Java 14. Note, however, that it is only provided in the Incubator version.

This packaging tool should allow developers to create their own Java applications, based on the JavaFX Javapackager packaging tool. Its main features are as follows:

1. Support local packaging format to provide users with natural installation experience;

2. You can specify startup parameters during packaging.

3. Start using the command line or a program using the ToolProvider API.

5. A composite garbage collector is deprecated

The latest and most anticipated innovation in Java 14 is clearly not for all Java developers; in fact, it intends to scrap the ParallelScavenge + SerialOld garbage collector combination.

JEP 366, which supports this change, makes it clear that their intention is not to remove the combination, but to discard it.

This combination of algorithms is deprecated because it is rarely used and requires a lot of maintenance work.

6. Conclusion

Java 14, scheduled for release in March 2020, will have relatively little impact on the day-to-day work of Java developers. At the forefront is instanceof pattern matching, a new feature that most developers are eager to try out.

However, you’ll have to be patient before using these features in a production environment, as they only appear as preview features in Java 14.

The good news is that Instanceof’s pattern matching represents the first step toward more extensive pattern matching in Java 15, 16, or 17.

All of these changes make developers very happy, because all of them will benefit significantly in terms of readability and personal development efficiency.