Page 1 of 1

About Question enthuware.ocpjp.v7.2.1263 :

Posted: Tue Jul 22, 2014 1:53 am
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

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

Posted: Tue Jul 22, 2014 6:34 am
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?

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

Posted: Tue Jul 22, 2014 7:13 pm
by NotSoOldNick
Excellent, that's the clarification I was after. Thanks a lot. :)

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

Posted: Fri Jul 24, 2015 9:12 am
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?

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

Posted: Fri Jul 24, 2015 7:55 pm
by admin
Yes, compiler infers the type of ArrayList as ? i.e. the unknown type from the declaration List<?>.

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

Posted: Thu Aug 20, 2015 5:00 pm
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 <?>

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

Posted: Thu Aug 20, 2015 7:53 pm
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>

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

Posted: Fri Aug 21, 2015 2:30 am
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));

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

Posted: Fri Aug 21, 2015 4:34 am
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.