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:

At first glance, although this removes the type information from:

it’s actually not very compelling when you consider Java 8 already let us remove the type information altogether:

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:

With the ability to use var on parameters in a lambda expression, this can be shortened in Java 11 to:

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!

This entry was posted in Releases and tagged , . Bookmark the permalink.

12 Responses to Java 11 in IntelliJ IDEA 2018.2

  1. .w says:

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

    • .x says:

      rather not

    • Trisha Gee says:

      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. Pingback: IntelliJ IDEA 2018.2 Beta 版本发布,支持 Java 11 – 技术成就梦想

  3. Pingback: IntelliJ IDEA 2018.2 Beta 版本发布,支持 Java 11 – 开源中国社区 – 技术成就梦想

  4. Pingback: IntelliJ IDEA 2018.2 released with Java 11 support and Spring Boot improvements - SD Times

  5. Pingback: IntelliJ IDEA 2018.2 released with Java 11 support and Spring Boot improvements - Software CreatorsSoftware Creators

  6. Pingback: IntelliJ IDEA 2018.2 released with Java 11 support and Spring Boot improvements | Premium Blog! | Development code, Android, Ios anh Tranning IT

  7. Imaskar says:

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

  8. kalix says:

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

  9. Thiago dos Santos Hora says:

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

Leave a Reply

Your email address will not be published. Required fields are marked *