Arrays.asList() is declared as below.
@SafeVarargs
@SuppressWarnings("varargs")
public static <T> List<T> asList(T... a) {
return new ArrayList<>(a);
}
So, as you know, to initialise a concrete List with an array, you can do this:
String[] strings = new String[] {"hello", "world"};
List<String> list = new ArrayList<>(Arrays.asList(strings));
But what about this?
List<String> list = new ArrayList<>(new String[]{"hello", "world"});
I expected it would works, but didn't.
I understand why. It's because one of the constructors of ArrayList demands Collection class.
Then, what about Arrays.asList() ?
Varargs are compiled as array, so maybe that method would be compiled as below.
public static <T> List<T> asList(T[] a) {
return new ArrayList<>(a);
}
But it actually returns ArrayList object. Why is this possible?