From Java 8 to Java 15 in Ten Minutes

From Java 8 to Java 15 in Ten Minutes:

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, you can use Streams as follows:

Stream.of("hello", "great")
    .map(s -> s + " world")
    .forEach(System.out::println);
> hello world
> great world
Enter fullscreen mode Exit fullscreen mode

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

Streams can work on ListsSets, 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:

Optional.of(new Random().nextInt(10))
    .filter(i -> i % 2 == 0)
    .map(i -> "number is even: " + i)
    .ifPresent(System.out::println);
> number is even: 6

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:

$ /bin/jshell
jshell> System.out.println("hello world")
hello world

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:

jshell> List list = Arrays.asList(1, 2, 3, 4)
list ==> [1, 2, 3, 4]

This is now simplified as follows:

jshell> List list = List.of(1, 2, 3, 4)
b ==> [1, 2, 3, 4]

This fancy of(...) method exists for ListSet 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.

jshell> var x = new HashSet()
x ==> []

jshell> x.add("apple")
$1 ==> true

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:

Main.java:

public class Main {
  public static void main(String[] args) {
    System.out.println("hello world");
  }
}
$ java ./Main.java
hello world

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:

jshell> var i = 3
jshell> String s;
jshell> switch(i) {
   ...>     case 1: s = "one"; break;
   ...>     case 2: s = "two"; break;
   ...>     case 3: s = "three"; break;
   ...>     default: s = "unknown number";
   ...> }
jshell> s
s ==> "three"

In contrast, the new switch expression returns a value:

jshell> var i = 3;
jshell> var x = switch(i) {
   ...>     case 1 -> "one";
   ...>     case 2 -> "two";
   ...>     case 3 -> "three";
   ...>     default -> "unknown number";
   ...> };
x ==> "three"

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:

public class Main
{ 
  public static void main(String [] args)
  {
    var s = """
{
"recipe": "watermelon smoothie",
"duration": "10 mins",
"items": ["watermelon", "lemon", "parsley"]
}""";
    System.out.println(s);
  }
}

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

java --enable-preview --source 13 Main.java

{
    "recipe": "watermelon smoothie",
    "duration": "10 mins",
    "items": ["watermelon", "lemon", "parsley"]
}

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!🙌🎉

jshell> record Employee (String name, int age, String department) {}
|  created record Employee

jshell> var x = new Employee("Anne", 25, "Legal");
x ==> Employee[name=Anne, age=25, department=Legal]

jshell> x.name()
$2 ==> "Anne"

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:

Object obj = new String("hello");
if (obj instanceof String) {
  System.out.println("String length: " + ((String)obj).length());
}

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:

Object obj = new String("hello");
if (obj instanceof String) {
  System.out.println("String length: " + obj.length());
}

Sealed Classes (Java 15)

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

public sealed interface Fruit permits Apple, Pear {
    String getName();
}

public final class Apple implements Fruit {
    public String getName() { return "Apple"; }
}

public final class Pear implements Fruit {
    public String getName() { return "Pear"; }
}

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’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 ScalaKotlin and Python.

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

Wrap Up

Java has gone a long way in the last 6 years, there’s 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).

from Tumblr https://generouspiratequeen.tumblr.com/post/636932910023426048

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s