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:
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:
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!
.w says:
June 22, 2018Hi,
I came here from twitter post (https://twitter.com/intellijidea/status/1010157430976778242) but… would you consider using something more open, e.g. Mastodon (http://mastodon.technology/)? 🙂
.x says:
June 25, 2018rather not
Trisha Gee says:
June 26, 2018Is 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.
IntelliJ IDEA 2018.2 Beta 版本发布,支持 Java 11 – 技术成就梦想 says:
June 27, 2018[…] Java 11 support: Learn more […]
IntelliJ IDEA 2018.2 Beta 版本发布,支持 Java 11 – 开源中国社区 – 技术成就梦想 says:
June 27, 2018[…] Java 11 support: Learn more […]
IntelliJ IDEA 2018.2 released with Java 11 support and Spring Boot improvements - SD Times says:
July 26, 2018[…] for variables inside methods, now Java 11 lets us use this in lambda expressions,” the company wrote. It also now shows data flow information in the editor, shows type hints for long method chains in […]
IntelliJ IDEA 2018.2 released with Java 11 support and Spring Boot improvements - Software CreatorsSoftware Creators says:
July 26, 2018[…] for variables inside methods, now Java 11 lets us use this in lambda expressions,” the company wrote. It also now shows data flow information in the editor, shows type hints for long method chains in […]
IntelliJ IDEA 2018.2 released with Java 11 support and Spring Boot improvements | Premium Blog! | Development code, Android, Ios anh Tranning IT says:
July 26, 2018[…] for variables inside methods, now Java 11 lets us use this in lambda expressions,” the company wrote. It also now shows data flow information in the editor, shows type hints for long method chains in […]
Imaskar says:
August 23, 2018I can’t add jdk-11 to my Idea 2018.2.3 EAP Community. It says ‘Cannot find jdk classes’.
Trisha Gee says:
September 24, 2018Hi, sorry for the late response here – are you still having issues?
kalix says:
September 28, 2018is it possible now to run javafx 11 with jdk 11 on intellij ide?
Thiago dos Santos Hora says:
September 29, 2018Same here I’m getting this message : The selected directory is not a valid home for JDK
Björn Kautler says:
December 3, 2018Should 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 says:
January 23, 2019Well, 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.
Gagan Sharma says:
January 3, 2019hi… 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 says:
January 23, 2019Hi 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.