Java 11 in IntelliJ IDEA 2018.2

The next version of Java is due out in September, and IntelliJ IDEA 2018.2 is ready for it.  The main code feature in Java 11 is Local-Variable Syntax for Lambda Parameters – Java 10 gave us the ability to use the var keyword for variables inside methods, now Java 11 lets us use this in lambda expressions:

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

At first glance, although this removes the type information from:
BiConsumer<Processor, String> consumer = (Processor x, String y) -> x.process(y);

it’s actually not very compelling when you consider Java 8 already let us remove the type information altogether:
BiConsumer<Processor, String> consumer = (x, y) -> x.process(y);

But one of the cases where we might want to use var instead of the full type is if we wanted to apply an annotation to the parameter.  Annotations require us to declare the full type:
BiConsumer<Processor, String> consumer = (@NotNull Processor x, @Nullable String y) -> x.process(y);

With the ability to use var on parameters in a lambda expression, this can be shortened in Java 11 to:
BiConsumer<Processor, String> consumer = (@NotNull var x, @Nullable var y) -> x.process(y);

IntelliJ IDEA supports this change as you’d expect, allowing the var keyword in the lambda. But IntelliJ IDEA 2018.2 also helps migrate the code to this updated syntax.  If, for example, you were using annotations in a lambda expression, the inspection for removing the “redundant parameter types” will replace the types with var.

Along with updates for the use of var in Java 11, IntelliJ IDEA 2018.2 also has some improvements for working with var. Holding down Ctrl/ and hovering over the var keyword will show the type of the variable:Var type

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.

It’s important to remember when working with this new syntax that all relevant type information needs to be on the right of the equals sign since the left side no longer declares the type. There are two inspections that have been updated to reflect this.

Firstly, IntelliJ IDEA 2018.2 understands that while sometimes an initializer is not required for a variable with an explicit type, variables created with var will use an initial value to determine the type.  Therefore any “redundant initializer” warning that applies to explicit type variables is not shown for var:

Similarly, the removal of an array type is not suggested for var since it’s required:

No misleading warnings

In summary, IntelliJ IDEA 2018.2 is not only able to help when using Java 10 features, it’s ready to help us start coding with Java 11 before it’s even released!

Comments below can no longer be edited.

11 Responses to Java 11 in IntelliJ IDEA 2018.2

  1. Avatar

    .w says:

    June 22, 2018

    I came here from twitter post ( but… would you consider using something more open, e.g. Mastodon ( 🙂

    • Avatar

      .x says:

      June 25, 2018

      rather not

    • Trisha Gee

      Trisha Gee says:

      June 26, 2018

      Is the question: would we consider using Mastodon instead of Twitter? At this stage probably not, because we have a wide social network on Twitter, and for keeping as many people as possible up to date a platform with wide reach is really important.

  2. Avatar

    Imaskar says:

    August 23, 2018

    I can’t add jdk-11 to my Idea 2018.2.3 EAP Community. It says ‘Cannot find jdk classes’.

    • Trisha Gee

      Trisha Gee says:

      September 24, 2018

      Hi, sorry for the late response here – are you still having issues?

  3. Avatar

    kalix says:

    September 28, 2018

    is it possible now to run javafx 11 with jdk 11 on intellij ide?

  4. Avatar

    Thiago dos Santos Hora says:

    September 29, 2018

    Same here I’m getting this message : The selected directory is not a valid home for JDK

  5. Avatar

    Björn Kautler says:

    December 3, 2018

    Should it really be “In summary, IntelliJ IDEA 2018.2 is not only able to help when using Java 10 features”? I mean the “not” should probably be “now”, shouldn’t it? And maybe Java 11, not Java 10.

    • Trisha Gee

      Trisha Gee says:

      January 23, 2019

      Well, the whole sentence reads:

      “In summary, IntelliJ IDEA 2018.2 is not only able to help when using Java 10 features, it’s ready to help us start coding with Java 11 before it’s even released!”

      Which means “not only can you use Java 10, but you can also use Java 11”. So it is syntactically and semantically correct.

  6. Avatar

    Gagan Sharma says:

    January 3, 2019

    hi… i’m getting an error like “Error:java: release version 11 not supported” while i build my code in intellij idea 2018.3.2
    plz give me a solution.

    • Trisha Gee

      Trisha Gee says:

      January 23, 2019

      Hi Gagan, please log an issue in YouTrack with more details of what you’ve been trying to do and the errors that you’re getting. YouTrack is a much better place to get support.

Discover more