Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jshell in intellij doesn't allow generic types

I just started using jshell in intellij idea community version. When I write the below code in jshell, it works.

List<String> list = List.of("a", "b", "c");
System.out.println(list);

However the same code doesn't work in intellij. It says "Expression expected". It executes fine but shows that there is error with List<String>. The problem is "auto-complete" doesn't work. To workaround this issue, we can use raw type but I want a generic one. Is there something that I am missing? How can I write a generic type?

like image 903
Need Help Avatar asked Jun 14 '20 02:06

Need Help


People also ask

How to use Generics in IntelliJ?

Convert Raw Types to Generics refactoring If you want to apply generics to a single code fragment, select one in the editor. On the main menu, or on the context menu, select Refactor | Convert Raw Types to Generics. In the dialog that opens, define the refactoring options. Preview and apply changes.

How do I open JShell in Intellij?

Open the JShell ConsoleFrom the main menu, select Tools | JShell Console.

What is Generify in Java?

also What is Generify in Android? Generify refactoring The Generify refactoring is designed to transform existing code that does not use Generics, into the Generics-aware code. The refactoring analyzes existing code, and for each raw type creates safe and consistent parameter type.


1 Answers

Not an IntelliJ IDEA expert but I get my way around by writing a anonymous class:

new Object() {
    void method() {
        List<String> list = List.of("a", "b", "c");
        System.out.println(list);
    }
}.method();
like image 87
Aniket Sahrawat Avatar answered Oct 15 '22 04:10

Aniket Sahrawat