The whole concept of raw types was introduced for compatibility with pre-Generics code and consequently, using a raw type will effectively turn Generics off for all uses of this type, whether the generic signatures are related to the type’s declared type parameters or not.
This can be illustrated by the followed code example, which produces a compilation error before Java 5 and continues to produce a compilation error:
ArrayList list = new ArrayList();
String[] str = list.toArray(new String[0]);
in contrast to the generic code
ArrayList<Number> list = new ArrayList<Number>();
String[] str = list.toArray(new String[0]);
which can be compiled without errors, showing that the type parameters of toArray
and the List
are unrelated (a limitation of the generic type system).
Likewise, when you use new Cosmos()
, the result is a raw type and invoking says
on it will not use the generic type signature. But you don’t need to specify an actual type:
new Cosmos<>().says(s -> System.out.println(s.length()));
will compile without errors.