Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Generics K&B Page 638 Question 5 Code

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Here is the code from Generics K&B Page 638 Question 5
This class is to be updated to make use of appropriate generic types, with no changes in behavior
(for better or worse). Which of these steps could be performed? (Choose three.)
Correct answers are
B. Replace line 13 with
private Map<String, Integer> accountTotals = new HashMap<String, Integer>();
E. Replace lines 17–20 with
Integer total = accountTotals.get(accountName); //Type mismatch: cannot convert from Object to Integer
if (total == null) total = 0;
return total;

How come Option "E" is correct..I am getting Type mismatch: cannot convert from Object to Integer error..Please advice
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you done step B yet?
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did not do it because question is not clear. If I keep B step, I will not get error for Option E..Please let me know why should I keep Step "B"
Duc Vo wrote:Have you done step B yet?
 
Duc Vo
Ranch Hand
Posts: 254
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's actually quite clear. From the question:

Which of these steps could be performed? (Choose three.)

Which means you need to perform all three steps.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I need to insert the 3 options independently..Thanks Duc
 
Glen Iris
Ranch Hand
Posts: 172
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unrelated query on the same question:

The book says:

"D is wrong because an int cannot autobox to null".

FYI - option d:



I accept that this is correct as we are unboxing an Integer to an int. However, I was not aware that this cannot be null. can somebody please explain why. Is this true for all unboxing?
 
dennis deems
Ranch Hand
Posts: 808
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Glen Iris wrote:Unrelated query on the same question:

The book says:

"D is wrong because an int cannot autobox to null".

FYI - option d:



I accept that this is correct as we are unboxing an Integer to an int. However, I was not aware that this cannot be null. can somebody please explain why. Is this true for all unboxing?


An Integer can be null, but not an int. If we attempt an autoboxing assignment using a null reference, an exception will be thrown. This is true for all wrappers.
 
Glen Iris
Ranch Hand
Posts: 172
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again Dennis, thank you. Its something simple that I didnt know as I had never tried it in practice.
 
Bert Bates
author
Sheriff
Posts: 8900
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an informal standard on the real exam that we try to follow in the book:

If the question wants you to try only one answer at a time, like inserting a line of code, it should say something like:

"Which fragment, inserted independently at line X, compiles?"

The key phrase to look for is the "inserted independently". This isn't always present, but it usually is.

hth,

Bert
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic