What’s New in Java 14

JDK 14

March 17th was the release of JDK 14. The release is not LTS, but it contains several interesting new features. Let’s go over them.

 

Informative NullPointerException

It finally happened. Now it’ll become a little clearer what went wrong.

class Bar{
   void print(){
       System.out.println("Bar");
   }
}


class Foo{
   Bar bar;
}

..

Foo foo = new Foo();
foo.bar.print();
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Main$Bar.print()" because "<local2>.bar" is null

    at Main.main(Main.java:18)

You need to run it with flags -XX:+ShowCodeDetailsInExceptionMessages --enable-preview --source 14

New switch-case

With the new version, a new variation of the switch statement appears. The main feature is the the ability to specify multiple expressions in a case and a compilation error if all outcomes are undefined. Here are a few examples to make it more clear:

Multiple options:

switch (animal) {
   case CAT -> System.out.println("Cat");
   case COW, DOG ->  System.out.println("Cow or Dog");
}

Returning a value from switch:

var result =  switch (animal) {
     case CAT -> "Cat";
     case COW, DOG ->  "Cow or Dog";
     default -> "I don't know";
 };

Compilation error when not all possible outcomes are processed in the case. It only works when we return a value from switch:

var result = switch (animal) {
     case CAT -> "Cat";
     case COW ->  "Cow";
}

We get an exception:

Main.java:10: error: the switch expression does not cover all possible input values
      switch (animal) {
                    ^

Default value:

switch (animal) {
  case CAT -> System.out.println("Cat");
  case COW, DOG ->  System.out.println("Cow or Dog");
  default -> System.out.println("I don't know");
}

 

Records

This is a new concept in Java. In essence, Record is a class purpose of which is to store only data, without any behavior (an analogue of the structure). Access methods like equals, hashCode and other methods are automatically generated for this class. 

public record Address(String city, String street)

It is necessary to compile with the options —enable-preview —release 14

Text Blocks

We all know how inconvenient it is to define multi-line text constants inside the code. The ability to write text blocks appeared in the 13th version of JDK , but the feature is still in the Preview stage, although Second. Only a couple of small changes were added. 

String sql = """  
    SELECT * FROM “person”    
    WHERE id > 42             
    ORDER BY “created”    """;

Pattern matching for instanceof

Also a useful thing. It leads the object to the desired type and allows it to be used without any casts.

if (obj instanceof Foo f) 
{    
   //without cast  
    f.bar() 
}

Removing CMS GC

Concurrent Mark Sweep (CMS) Garbage Collector is unavaliable starting JDK 14. In case you try to use it, the JVM will issue a warning and start the virtual machine with the default GC

Other new features can be checked here  

To conclude:

New convenient features that appeared in the language are the ones, which are already present in other JVM languages (Kotlin and Scala). This is a positive development. For example

In addition, while skimming the documentation, I came across the mention of sealed classes (sealed in Kotlin). Who knows, maybe in the near future we will migrate back from Kotlin to Java. 🙂

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Back to top