The best new features in the Java programming language: Reasons to upgrade from Java 8

The most useful new language features in Java 9, 10, 11 and 12.

The best programming languages to learn in 2019: Top coding skills that pay you the most These are the languages that are in the highest demand and offer the highest salaries.

Java 8 may have come out in March 2014 but developers aren't in any rush to move off it.

Special Report

Riding the DevOps revolution

You can download all of the articles in this special report in one PDF (free registration required).

Nearly 80% of Java developers are still using Java 8, according to a quick poll of more than 1,300 devs by veteran Java developer and JetBrains developer advocate Trisha Gee.

With Java 12 due out in about one week's time, pressure is mounting on developers to move to a newer version of Java, particularly with Oracle ending public updates for Java 8 for business users in January this year.

At the at the QCon London 2019 developer conference Gee outlined some of the best new features added to the language since Java 8 that might persuade developers to make the jump.

SEE: Job description: Java developer (Tech Pro Research)

These are the language features that Gee highlighted as being the most promising reasons to upgrade to a newer version of Java.

JShell

When was it introduced? Java 9

What it does: JShell provides a command line at which developers can experiment with running small snippets of Java, offering a simple way to test new features, allowing developers to create variables, define methods, and to save scripts into a file.

Var

When was it introduced? Java 9

What it does: Var cuts down on unnecessary code and increases readability by preventing developers from having to repeatedly declare a type within code.

The feature can be useful where developers would otherwise have to declare the type on both sides of the = operator, for example when using generics:

ArrayList<Person> people = new ArrayList<Person>();

could be written as:

var people = new ArrayList<Person>();

Convenience factory methods for collections

When were they introduced? Java 9

What they do: Allows developers to create a genuinely unmodifiable Lists and Sets, using simpler syntax.

Earlier versions of Java used to require developers to declare List using Arrays.asList("element1", "element2") , which in turn had to be wrapped using the Collections.unmodifiableList() method, says Gee.

The code for achieving the same outcome is far more straightforward from Java 9 onwards, simply requiring calling the List.of() method, for example, List.of("element1","element2").

Similarly, creating a Set used to be cumbersome, but can now be carried out using the Set.of() method.

Creating a Map is also simpler, no longer requiring a static initializer but instead being possible using a Map.ofEntries()method call, or a Map.of() method call if there are fewer than 10 entries, which simply requires entering key-value pairs.

"It's a bit less boilerplate and a bit more readable, it's particularly useful for things like test code," says Gee.

New methods on the stream API

When were they introduced? Java 9

What they do: There are two new methods, takeWhile(), which allows a developer to specify that the data in a stream should be processed until a certain condition is met, and dropWhile(), which allows a developer to specify that the data in a stream should be ignored until a certain condition is met.

For example, if a developer wanted to process data until the user count hit a certain threshold:

items.Stream()

.takeWhile(user -> user.count() < maxCount)

.forEach(user -> position.incrementAndGet());

Collect into unmodifiable lists from streams

When was it introduced? Java 10

What it does: Gives developers the ability to automatically populate unmodifiable lists from a stream using simple syntax, for example:

items.stream()

.filter(Objects::nonNull)

.map(Object::toString)

.collect(Collectors.toUnmodifiableList());

Jigsaw / Java Module System

When was it introduced? Java 9

What it does? Jigsaw is useful for Java software library developers, allowing them to encapsulate code that is not supposed to be altered.

Gee says it is also a good for for enterprise developers who want to properly separate the concerns inside their applications.

JLink

When was it introduced? Java 9

What it does? JLink allows developers to package up individual Java modules, just the bits of Java their application needs, into a small deployable runtime.

This provides a method for deploying smaller applications than would be the case when using a traditional Java Runtime Environment, says Gee, useful when deploying software in the cloud or in containers.

New methods on Optional

When were they introduced? Java 9, 10, 11

What they do? Gee describes Optional as a way of hedging your bets when setting the return type of a method, of saying this method "might return something, it might not" by setting it to return an Optional container.

These new methods "just make Optional easier to work with," she added.

For example, developers can use the ifPresentOrElse() method to reduce the code needed to specify the actions to take whether there's anything in the Optional container, e.g. in the case of an Optional container named anOptional:

anOptional.ifPresentOrElse(

s -> System.out.println(s),

() -> System.out.println("Nothing found")

);

In addition, Optional's or() method can be used to return alternate optional values, the orElseThrow() method can return a default exception if not value is found, and the isEmpty() method checks whether the Optional container is empty.

Built-in HTTP client

When was it introduced? Java 11

What it does? Java 11 comes with a built-in HTTP client, removing the need for additional software dependencies, with the client being non-blocking, supporting reactive streams, as well as HTTP/1 and HTTP/2.

Switch expressions

When will it be introduced? Java 12

What it does? Due to be introduced with the release of Java 12 in about one week's time, switch expressions allow developers to use simpler syntax for switch statements.

For example, rather than each case within the switch statement using this type of syntax:

switch (port) {

case 20:

type = PortType.FTP;

break;

}

each case could instead be expressed the following manner:

Switch (port) {

case 20 -> PortType.FTP;

}

Also see