About Question enthuware.ocpjp.v7.2.1263 :

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

Moderator: admin

Post Reply
NotSoOldNick
Posts: 2
Joined: Mon Jun 30, 2014 12:11 am
Contact:

About Question enthuware.ocpjp.v7.2.1263 :

Post by NotSoOldNick »

Hi,

While I understand why both of the correct answers are correct, I'm struggling to figure out why

Code: Select all

List<?> list = new ArrayList<?>(Arrays.asList(names));
is incorrect, no direct explanation being given in the test. :|
Can someone enlighten me?

Thanks

Nick

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

Re: About Question enthuware.ocpjp.v7.2.1263 :

Post by admin »

ArrayList<?> means an ArrayList of unknown type. While it is ok to have a variable that is declared of this type, it is not ok to instantiate an object i.e. an ArrayList of unknown type. Indeed, if you don't know what your are going to put in your ArrayList, how can you create it?
If you like our products and services, please help us by posting your review here.

NotSoOldNick
Posts: 2
Joined: Mon Jun 30, 2014 12:11 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1263 :

Post by NotSoOldNick »

Excellent, that's the clarification I was after. Thanks a lot. :)

Danny Sheridan
Posts: 30
Joined: Sat May 02, 2015 4:48 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1263 :

Post by Danny Sheridan »

You can replace the type arguments required to invoke the constructor of a generic class
with an empty set of type parameters (<>)
as long as the compiler can infer the type arguments from the context.
So then this must include inferring with an argument of unknown type (ie: <?> )

For example I know this compiles
but I've been less than 100% about why

Code: Select all

List<?> myList = new ArrayList<>();
But is this the reason why it does?

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

Re: About Question enthuware.ocpjp.v7.2.1263 :

Post by admin »

Yes, compiler infers the type of ArrayList as ? i.e. the unknown type from the declaration List<?>.
If you like our products and services, please help us by posting your review here.

colmkav
Posts: 21
Joined: Thu Jul 16, 2015 4:22 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1263 :

Post by colmkav »

admin wrote:Yes, compiler infers the type of ArrayList as ? i.e. the unknown type from the declaration List<?>.
But then how is that any different to option 3? Option 1 and 3 seem to evaluate to the same, if <> is treated as <?>

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

Re: About Question enthuware.ocpjp.v7.2.1263 :

Post by admin »

<> is NOT treated as <?>. <> is just a syntactical shortcut to avoid writing the name of the type given on the other side of =. If the other side has <?>, <> is treated as <?>. If the other Side has <X>, <> is treated as <X>
If you like our products and services, please help us by posting your review here.

colmkav
Posts: 21
Joined: Thu Jul 16, 2015 4:22 am
Contact:

Re: About Question enthuware.ocpjp.v7.2.1263 :

Post by colmkav »

admin wrote:<> is NOT treated as <?>. <> is just a syntactical shortcut to avoid writing the name of the type given on the other side of =. If the other side has <?>, <> is treated as <?>. If the other Side has <X>, <> is treated as <X>
Sorry, I still don't understand. <?> IS on the other side for option 1. In option 1 <> on right side becomes <?> so option 1 and 3 become the same? But option 1 is deemed a correct answer and option 3 is deemed wrong

1) List<?> list = new ArrayList<>(Arrays.asList(names));

3) List<?> list = new ArrayList<?>(Arrays.asList(names));

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

Re: About Question enthuware.ocpjp.v7.2.1263 :

Post by admin »

Sorry, my mistake. My explanation above was not accurate. I apologize for that. There are two things here:

1. <?> really means that you don't know the type. This means that you cannot use <?> while instantiating a generic type. Java prohibits it because it doesn't make any sense. If you do new ArrayList<?>(), you are saying you don't know what type of objects you are going to add to it but instantiate it anyway. You should do new ArrayList() in that case.

2. While it is true that the type of <> is inferred by looking at the target. That is not the only factor that is used to resolve it. As per https://docs.oracle.com/javase/tutorial ... rence.html
Note: It is important to note that the inference algorithm uses only invocation arguments, target types, and possibly an obvious expected return type to infer types. The inference algorithm does not use results from later in the program.
Therefore, in this case, <> will not resolve to <?> (because that will error out as explained in point 1). It will be resolved by using the type of the argument Arrays.asList(names).

That is the reason why option 1 and 3 are different. While 1 is valid, 3 is not.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 222 guests