2

I want to create an empty list of type List<String> in jshell

I am hoping jshell supports some sort of syntax shorthand for example:

  List<String> args = []

However it failed with this error message:

|  Error:
|  illegal start of expression
|  List<String> args = [];
|                      ^

Of course I can just use the standard java expression:

import com.google.common.collect.ImmutableList
List<String> args = ImmutableList.of()

but I want to know if it can be more terse.

Naman
  • 27,789
  • 26
  • 218
  • 353
Anthony Kong
  • 37,791
  • 46
  • 172
  • 304

2 Answers2

3

If you're initialising a List you can do it as:

List<String> list = List.of();

If instead it was an array, you could have initialized it as :

String[] arr = {}
Naman
  • 27,789
  • 26
  • 218
  • 353
0

What about var keyword this comes with java 10:

var list = new ArrayList<String>(); // infers ArrayList<String>
Rafał
  • 59
  • 8