Page 1 of 1

About Question enthuware.ocpjp.v8.2.1293 :

Posted: Sun Aug 05, 2018 10:56 am
by __JJ__
Hi Admin

Code: Select all

HashMap<?, List<String>> box = new HashMap<?, List<String>>();
You cannot have a '?' on the right hand side. I.e. you must provide a type name while instantiating a typed class.
This compiles:

Code: Select all

HashMap<?, List<?>> box = new HashMap<Integer, List<?>>(); 
I bring this up only because otherwise one might be tempted to read your statement as "any appearance of ? on the RHS of = is illegal syntax and will not compile."

Thanks.

Re: About Question enthuware.ocpjp.v8.2.1293 :

Posted: Thu Feb 27, 2020 9:06 pm
by EricRMI
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! :)

Re: About Question enthuware.ocpjp.v8.2.1293 :

Posted: Thu Feb 27, 2020 10:07 pm
by admin
Yes, you are right. The actual instantiation requires a type.