NamedTypeParameterDRAFT
To instantiate a generic type such as HashMap<K,V>
, one has to specify the type parameter names and the types, e.g., such as new HashMap<String K, Integer V>
.
To instantiate a generic type, one needs to specify type parameter names as well as types
CorrectionHere is what's right.
To instantiate a generic type, one just plugs in the types at the position of the corresponding type parameters:
E.g., given the generic type HashMap<K,V>
, with the two type parameters K
(for “type of the keys”) and V
(for “type of the values”), if we want to have a type for a hash map from String
to Integer
, we instantiate the generic type by plugging in String
for K
and Integer
for V
:
HashMap<String, Integer> myMap;
The type of myMap
is, specifically, hash map from string to integer. It is not a generic hash map from whatever type of key to whatever type of value.
Note: This is somewhat analog to (but not the same as!) how one assigns values to formal parameters in method calls. The method is called by passing the actual parameter, and the formal parameter is not repeated. For example, given a method with the signature void m(int a)
, to call that method and pass along the actual parameter 3, we write m(3)
— not m(a 3)
. However, there is a fundamental difference: type parameters (used in generic types) are set to types, but method parameters are set to values.