About Question enthuware.ocpjp.v8.2.1293 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
__JJ__
Posts: 125
Joined: Thu Jul 05, 2018 6:44 pm
Contact:

About Question enthuware.ocpjp.v8.2.1293 :

Post 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.

EricRMI
Posts: 1
Joined: Sun Sep 15, 2019 1:30 pm
Contact:

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

Post 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! :)

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Yes, you are right. The actual instantiation requires a type.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 31 guests