• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

AutoBoxing/Unboxing

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Here is another question from K&B:

8. Given:

12. public class AccountManager {
13. private Map accountTotals = new HashMap();
14. private int retirementFund;
15.
16. public int getBalance(String accountName) {
17. Integer total = (Integer) accountTotals.get(accountName);
18. if (total == null)
19. total = Integer.valueOf(0);
20. return total.intValue();
21. }
23. public void setBalance(String accountName, int amount) {
24. accountTotals.put(accountName, Integer.valueOf(amount));
25. } }
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.)

A. Replace line 13 with
private Map<String, int> accountTotals = new HashMap<String, int>();


B. Replace line 13 with
private Map<String, Integer> accountTotals = new HashMap<String, Integer>();

C. Replace line 13 with
private Map<String<Integer>> accountTotals = new HashMap<String<Integer>>();


D. Replace lines 17�20 with
int total = accountTotals.get(accountName);
if (total == null) total = 0;
return total;


E. Replace lines 17�20 with
Integer total = accountTotals.get(accountName);
if (total == null) total = 0;
return total;


F. Replace lines 17�20 with
return accountTotals.get(accountName);


G. Replace line 24 with
accountTotals.put(accountName, amount);


The correct answers are B,E and G.
If E is correct, what's wrong with F? Cant the return value of accountTotals be unboxed on the fly?

The answer explaination says: "F is wrong because a null can't unbox to 0"

Can someone please decode this for me? What's null in here and where is do we have 0?

regards,
SCJPAspirant
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
E is correct but F is wrong because

in choice E:

E. Replace lines 17�20 with
Integer total = accountTotals.get(accountName);
if (total == null) total = 0;
return total;

the total is checked whether its null
if its null, then total will be 0

whereas in choice F:

Replace lines 17�20 with
return accountTotals.get(accountName);

there is not any checking whether the total is null
if the total is null, the value returned for the method "getBalance" which is supposed to be "int" IS NOT compatible with the "null" value of the total. and "null" cannot be unbox to 0

hope that helps
 
Ravi Yadav
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh i missed that. Thanks Ailina.
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

you are supposed to use your real name here, please see :
http://www.javaranch.com/name.jsp

Gilles
 
Ravi Yadav
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marceau.
 
reply
    Bookmark Topic Watch Topic
  • New Topic