I am an absolute Java newbie and just got started learning Java. I came across this JShell functionality within IntelliJ where one can write some quick code without writing any class constructs. But the problem which annoys me for quite some time is that when I initialized even a simple ArrayList, it seems to be working fine but it always gives me this "Expression expected" error which underlines List<Integer>
in red.
I do not encounter such error while in a normal .java file but only in JShell.
Does anyone know why? Much appreciated in advance!
This is an IntelliJ bug, and has been reported in IDEA-221953 and IDEA-191768 for example. The editor doesn't seem to understand generics (among other things like var
) when they appear in code snippets.
Note that this isn't the case if you write the statement inside a method,
static void foo() {
List<Integer> foo = new ArrayList<>();
// ...
}
foo();
but of course, that sort of defeats the purpose of using JShell :(
Running the code itself isn't affected. You can still run the code by pressing cmd/ctrl + enter, and it will work as you expect, despite all the red squiggles.