I'm looking for some clarification on this, because I am someone who DID (briefly) interpret the statement as "any appearance of ? on the RHS of = is illegal syntax and will not compile." and I'm trying to make sure I understand correctly.
Question: Would it be correct to say the following:
When instantiating a generic type, you must specify a class name. However, if that class name is itself a generic type, this generic type itself may use a wildcard in its declaration.
This means:
Code: Select all
HashMap<?, List<String>> box = new HashMap<?, List<String>>();
is
invalid, because the instantiation of new HashMap must specify a type for its key.
But it also means:
Code: Select all
HashMap<?, List<?>> box = new HashMap<Integer, List<?>>();
is
valid, because in this context the HashMap is instantiated to map Integer to List of 'unknown' type. This works because although HashMap is being instantiated, neither Integer nor List<?> are being instantiated at this time, they are placeholders.
Is this a correct summary of the pertinent rules?
Thank you for your time in responding!