Page 1 of 1

About Question enthuware.ocpjp.v8.2.1825 :

Posted: Thu Nov 05, 2015 2:12 pm
by 361gradus
Could you please explain it to me why in the fifth case we can use wildcards?

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

Posted: Thu Nov 05, 2015 8:45 pm
by admin
If you look at the right hand side of the assignment, you will notice that it is not using generics at all. This means that it is the programmers responsibility to make sure the object created complies with the requirements of the type of the variable. The compiler will not check for type safety. This is an unsafe operation.

Therefore, the compiler will accept even the following statement:
PlaceHolder<Integer, String> ph5 = new PlaceHolder(10, 10); //5

HTH,
Paul.

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

Posted: Wed Dec 16, 2015 2:24 pm
by mrmuiz
You can specify generic types only when you use the new operator.
I think that's not exactly true, don't ask me why (I was messing with the code) but this compiles

Code: Select all

PlaceHolder<String, String> ph2 = PlaceHolder.<String>getDuplicateHolder("b");

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

Posted: Mon Dec 21, 2015 3:51 am
by admin
Removed the statement. It is too loaded.
thank you for your feedback!