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

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

Moderator: admin

Post Reply
fuss

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

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

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

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

Post 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.
If you like our products and services, please help us by posting your review here.

Guest

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

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

chang_surrey
Posts: 7
Joined: Wed Dec 04, 2013 5:07 pm
Contact:

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

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

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

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

Post 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?
If you like our products and services, please help us by posting your review here.

chang_surrey
Posts: 7
Joined: Wed Dec 04, 2013 5:07 pm
Contact:

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

Post by chang_surrey »

I see your point - an excellent explanation on what is "absolutely necessary" in using generics.

Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests