Java 11 and IntelliJ IDEA

Posted on by

This week Java 11 was released! It feels like only yesterday that we was saying the same thing about Java 9. This new six monthly release cadence is a big change for the Java community, and a welcome one – Java developers are getting small drops of interesting new features regularly.

Java 11

Java 11, like Java 10 before it, has a fairly short list of new features, which is a good thing for us developers as it’s much easier to see what may be interesting and useful to us. From an IntelliJ IDEA point of view, there’s really only one feature that benefited from some extra support in the IDE, and that was JEP 323: Local-Variable Syntax for Lambda Parameters. We’ve already blogged about this in the context of Java 11 support in IntelliJ IDEA 2018.2, but let’s cover it again quickly.

When lambda expressions were introduced in Java 8, you could write something like this:



BiConsumer<Processor, String> consumer1 = (Processor x, String y) -> x.process(y);

The type information is included for the x and y parameters. But you didn’t need to include this type information as it was already known – in this case, the BiConsumer on the left declares these two types with generics. IntelliJ IDEA lets you remove these types if you wish:

Java 10 introduced var for local variable types, which we’ll talk about a bit later in this post, and Java 11 took this further to allow var on lambda expression parameters. The main use case for this is when a parameter requires an annotation. Annotations appear next to the type, so prior to Java 11 this would have meant code with an annotation might look something like:



BiConsumer<Processor, String> consumer = (@NotNull Processor x, @Nullable String y) -> x.process(y);

var

In Java 11, we can make this a little shorter usinginstead of the parameter types, and IntelliJ IDEA can do this conversion for you. Note that this is suggested when you press Alt+Enter on the type, it’s not flagged as a warning in the code.

Java 11: Be aware, the APIs you use may not be there any more

As well as new language features, it’s important to understand that Java 11 actually removes features. This step not only affects deprecated features and functionality that wasn’t used much, it also aims to simplify the core of the language by moving some large sections into separate dependencies (e.g. JavaFX), or expecting applications to use external dependencies that were already available (e.g. Java EE).

Both the Java EE and CORBA modules have been removed. While CORBA is probably not highly used, many applications do, of course, make use of Java EE. Usually this is in the context of an application server, or some other specific implementation of Java EE, but some applications and libraries make use of small sections of Java EE for specific purposes. For example JAXB is now not in the core language, you’ll need to add a specific dependency on it. There’s more information on possible replacement dependencies on this StackOverflow question.

Java 10

Java 10 was released only six months ago, and many of us may not have started using it yet.

As a reminder, the main new feature from Java 10 was the introduction of var, which, as we saw above, lets us use var instead of a specific type. This is not introducing dynamic typing into Java, instead it’s continuing a trend of reducing boilerplate in Java, similar to the introduction of the diamond operator which meant we no longer had to declare generic types on both sides of the equals sign.

IntelliJ IDEA supports var in a number of ways. Firstly, inspections give you the option of replacing types with var , or var with types.

By default the inspection won’t give you a warning about code that can use var (or code that should have an explicit type), but as usual the inspection can be configured according to your team’s style.

IntelliJ IDEA can also help you to navigate code that uses var . Holding down Ctrl/⌘ and hovering over var will show the type of the variable.

Like any other type, we can click here and navigate to the declaration, or we can use Ctrl+B/⌘B to navigate to the declaration via var . We can also use Quick Documentation (Ctrl+Q/F1) or Quick Definition (Ctrl+Shift+I/⌥Space) on var to see the type.

We covered using var in quite a lot of depth in our webinar on IntelliJ IDEA and Java 10.

Java 10 also came with a few nice additions to Optional and Collectors, so if you use the Streams API it’s worth having a look at these new methods.

Java 9

Last September’s Java 9 release was a big one, and people may be surprised to learn that both 10 and 11 effectively replace 9 – some JDK providers (e.g. Oracle) will not be offering long term support for Java 9 (or Java 10). Teams looking to jump straight from Java 8 to Java 11, skipping out the versions without long term support, still need to understand the changes that came in to Java 9 as obviously they’ll be part of Java 11.

We have already covered Java 9 and IntelliJ IDEA a number of times on this blog, and we have a recording of a webinar which covers many Java 9 features that may be interesting to developers. Of course modularity is the most famous feature, but there are lots of other additions, including the new Convenience Factory Methods for Collections. Personally this is my favorite feature from Java 9, and conveniently IntelliJ IDEA inspections can offer to migrate code to use the new methods.

A note on migration

While the goal of this post has been to show features in IntelliJ IDEA that make working with Java 9, 10, and 11 easier, and not specifically to help developers to migrate their code to these versions, we can’t help but throw in a bit of advice in this area. If you are looking to use Java 11 in the near future, you should start by making sure all of your dependencies are up to date. Many JVM languages, libraries and frameworks had to make big changes to work with Java 9, and yet more to keep up with changes from Java 10 and Java 11. You should be able to update the versions of the libraries you’re using with minimal impact on your own application and be a significant step closer to being able to use the latest version of Java.

If you are interested in migrating from Java 8, I wrote a couple of articles on the topic elsewhere, specifically tackling migrating to Java 9 (which will, of course, apply also to Java 11):

Migrating from Java 8 to Java 9 for Oracle’s Java Magazine, using IntelliJ IDEA of course.

Painlessly Migrating to Java Jigsaw Modules – a case study for InfoQ, looking at introducing modularity to your application.

Java 11 may only have only just been released, but IntelliJ IDEA already fully supports it, and makes it easier to use the new features in Java 11, 10, and 9.

Try it out today! You can download the open source OpenJDK build (provided by Oracle) which is ready for production use now.

Please note that I have suggested the OpenJDK build here, as Oracle have changed their license and now produce a commercial and an open source JDK. Please do read this post for more information, it’s very important to understand.