Page 1 of 1

About Question com.enthuware.ets.scjp.v6.2.91 :

Posted: Mon Mar 14, 2011 6:22 pm
by fuss
Replacing the fragment:

Code: Select all

Map map = new HashMap();    //1
with the:

Code: Select all

Replace line //1 with Map<Book, Integer> map = new TreeMap<Book, Integer>();
give us a possibility to get rid of the unnecessary casting here:

Code: Select all

Integer i = (Integer) map.get(b); //4
so to my mind the answer:

Replace line //4 and //5 with:
Integer i = map.get(b);
return i == null? 0:i;

could also be a good choice.

Re: About Question com.enthuware.ets.scjp.v6.2.91 :

Posted: Tue Mar 15, 2011 6:13 am
by admin
Hi,
The question says, "Choose changes that are absolutely necessary to take advantage of generics." The change that you have mentioned is not necessary.

Re: About Question com.enthuware.ets.scjp.v6.2.91 :

Posted: Wed Nov 16, 2011 3:08 pm
by Guest
It depends on how you define "advantage of generics".
I would agree with fuss, that the advantage is also no need of casting.

Re: About Question com.enthuware.ets.scjp.v6.2.91 :

Posted: Tue Feb 04, 2014 6:41 am
by chang_surrey
I kinda agree here...because it was talked in Sierra Bates book that, before generics was introduced, people had to do casting like in line //4 and //5. But with generics, thanks to the compile-time type checking, people no longer need to do that.

Therefore, I think it is reasonable to say that "removing the need of unnecessary casting" is an advantage of using generics.

Re: About Question com.enthuware.ets.scjp.v6.2.91 :

Posted: Tue Feb 04, 2014 8:35 am
by admin
Not requiring an explicit cast is indeed an advantage of generics. No question about that. But what is required to be able to take that advantage? In other words, what must be done so that you could take that advantage?

Re: About Question com.enthuware.ets.scjp.v6.2.91 :

Posted: Wed Feb 05, 2014 4:36 am
by chang_surrey
I see your point - an excellent explanation on what is "absolutely necessary" in using generics.