Java 15 evolution

Bulruno
6 min readApr 21, 2021

--

This blog will give you samples of awesome new feature added since Java 7. I’ll showcase at least one major improvement for each Java version, all the way to Java 15 which was released in fall 2020. Java now fully supports lambdas, functional programming, type inference via var, immutable collections with simple constructors, and multi-line strings. Moreover, there are exciting new experimental features like data classes (record), and sealed classes. Finally I’ll talk about the Java REPL which offers high value for quick experiments.

Table of Contents:

· Functional Programming (Java 8)
· Streams (Java 8)
· Optional (Java 8)
· JShell (Java 9)
· Factory Methods for Immutable Collections (Java 9)
· Type Inference with var (Java 10)
· Single Source File Launch (Java 11)
· Switch Expression (Java 12: experimental, full feature: Java 14)
· Multi-line Strings (Java 13: experimental, full feature: Java 15)
· Data Classes: record (Java 14: experimental)
· instanceof without Cast (Java 14: experimental)
· Sealed classes (Java 15: experimental)
· Bonus: Updated Licensing Terms Starting with Java 8
· Wrap up

Functional Programming (Java 8)

In Java 8, functional programming and lambdas were added as language features. The two core paradigms of functional programming are immutable values and elevating functions to first class citizens. Data goes through a pipeline of modification steps, where each step takes some input and maps it to a new output. Functional programming can be used with Streams and null-safe monads (Optional) in Java as shown below...

Streams (Java 8)

For your average computer program, you often have to work with a list of values and perform a given transformation on each value. Prior to Java 8, you had to use a for loop for this transformation, but from now on, you can use Streams as follows:

The map function takes as input a lambda, which will be applied to all elements in the stream.

Streams can work on Lists, Sets, and Maps (via transformation). Thanks to Streams, you can get rid of pretty much all loops in your code!👌

Optional (Java 8)

Another common problem in Java were Null Pointer Exceptions. So, Java introduced Optional — a monad that wraps a reference which might or might not be null. Applying updates to this Optional can be done in a functional way:

In the snippet above, we create a random number, wrap it inside an Optional object, and then only print the number if it is even.

JShell (Java 9)

Finally, we have a REPL for Java, and its name is JShell!😊 In a nutshell, JShell allows to experiment with Java snippets without writing and compiling a full Java class. Instead, you can execute one command at a time, and you immediately see the result. Here’s a simple example:

Folks familiar with interpreted languages like JavaScript or Python have had the pleasure of a REPL for a long time, but so far, this feature was missing in Java. JShell allows to define variables, but also more complex entities like multi-line functions, classes, and perform loops. Moreover, JShell supports auto-completion, which comes in handy if you don’t know the exact methods offered by a given Java class.

Factory Methods for Immutable Collections (Java 9)

Simple initialization of Lists has been missing in Java for a long time, but those times are over now. 😅 Previously you had to do something like this:

This is now simplified as follows:

This fancy of(...) method exists for List, Set and Map. They all create an immutable object in just one simple line of code.

Type Inference with var (Java 10)

Java 10 introduced the new var keyword which allows to omit the type of a variable.

In the snippet above, the type of x can be inferred to be HashSet by the compiler.

This feature helps to reduce boilerplate code and improve readability. There’s some limitations to it though: you can only use var inside of method bodies, and the compiler will infer the type at compile time, so everything is still statically typed.

Single Source File Launch (Java 11)

Previously, when you had written a simple Java program consisting of one file, you had to first compile the file with javac and then run it with java. In Java 11, you can do both steps with one command.

First you define your single source file Main.java:

Now you can compile and run it in one step:

For simple starter programs or experiments consisting of just one Java class, this feature for launching single source files will make your life easier.

Switch Expression (Java 12)

Java 12 brought us Switch expressions. Here’s a quick showcase of how the expression differs from the old switch statement.

The old switch statement defines the flow of the program:

In contrast, the new switch expression returns a value:

To sum up, the old switch statement is for program flow, and the new switch expression resolves to a value.

Notice that this new switch statement is a sort of mapping function: there’s one input (in the above case i), and there’s one output (here x). This is actually a pattern matching feature which helps to make Java more compatible with the functional programming principles. A similar switch statement has been available in Scala for a while.

A couple of things to note:

  • Instead of double points, we use arrows ->
  • There’s no need for break
  • The default case can be omitted when all possible cases are considered
  • To enable this feature with Java 12, use --enable-preview --source 12

Multi-line Strings (Java 13)

Did you ever have to define a long multi-line String like JSON or XML? So far, you’d probably squash everything on one line and use newline characters \n, but this makes the String much harder to read. Here comes Java 13 with multi-line Strings!💪

Sample case:

Now, we run the main Method via single-file-launch:

The resulting String spans multiple lines, quotation marks "" are left intact, and even tabs \t are preserved!

Data Classes: record (Java 14)

Of all the new features in this article, this is probably the one I’m most excited about: finally, there are data classes in Java! These classes are declared with the record keyword and have automatic Getters, a constructor, and equals() method etc. In short, you can get rid of a huge chunk of boilerplate code!🙌🎉

Scala has a similar feature with case classes, and Kotlin with data classes. In Java, lots of developers used Lombok so far, which offered pretty much the features that now inspired records for Java 14. More details can be found in this Baeldung article.

instanceof without Cast (Java 14)

Prior versions of Java already contained the instanceof keyword:

The unfortunate part: First we check that s is of type String, then we cast it again to retrieve its length.

Now with Java 14, the compiler is smart enough to infer the type automatically after the instanceof check:

Sealed classes (Java 15)

With the sealed keyword, you can restrict which classes can extend a given class or interface. Here’s an example:

So how does this help us? Well, now you know how many Fruits there are. This is actually an important step into the direction fully supported pattern matching, where you can sort of treat classes like enums. This sealed feature goes together nicely with the new switch expression explained previously.

Bonus: Updated Licensing Terms Starting with Java 8

One last topic for this article: licensing. Most of you heard that Oracle stopped updates for Java 8 (for the free commercial version). So here are your options:

  • Use a newer Oracle JDK version (Oracle offers free security updates for only 6 months after each release)
  • Use and old JDK version at your own risk
  • Use an old OpenJDK Java version, those still get security updates from the open source community or a third party vendor
  • Pay Oracle for premier support (e.g. Java 8: support until 2030)

Below you can see the tentative Oracle support durations per JDK:

Oracle support timeline per JDK

Oracle’s new licensing model is influenced by the new release cycle: Oracle will bring out a new Java version every 6 months. This new release cycle helps Oracle to improve Java at a faster pace, get quicker feedback via experimental features, and catch up with more modern languages like Scala, Kotlin and Python.

If you’re interested in more licensing details, checkout this article.

Wrap up

Java has gone a long way in the last 6 years, there have actually been 8 new Java releases since then!🚀 All these awesome new features help to make Java a competitive option compared to other JVM-based rivals (Scala and Kotlin).

I had a blast writing this article🔥 Thanks for reading😊 What’s your favorite feature in a recent java release? I’d love to get a feedback from you! Cheers!

--

--