• 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

Generices explanation

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
H. Replace line 24 with
accountTotals.put(accountName, amount.intValue());
Answer:
B , E, and G are correct.
A is wrong because you can't use a primitive type as a type parameter. C is wrong because
a Map takes two type parameters separated by a comma. D is wrong because an int can't
autobox to a null, and F is wrong because a null can't unbox to 0. H is wrong because you
can't autobox a primitive just by trying to invoke a method with it.

what dose it mean that is wrong because an int can't
autobox to a null, and F is wrong because a null can't unbox to 0. H is wrong because you
can't autobox a primitive just by trying to invoke a method with it.?

i really dont get it
i wish any one could help


 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

D is wrong because an int can't
autobox to a null, ...

what dose it mean that is wrong because an int can't
autobox to a null,



With option D, you are changing the total parameter from an Integer object, to a primative int. This should be fine, since autoboxing will do the conversion for you... However, the next line hasn't been changed. The next line checks to see if the total variable is null, and if so, assign it to a value of zero.

This was needed when total was originally an Integer instance. Now that this is an int, it will generate a compile error.

Henry
 
Hossam El-Gazzaz
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you henery for replying
the souce is s&k chapter 7

what about the other worng answers i'cant get them too?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

and F is wrong because a null can't unbox to 0....

what dose it mean that is wrong because ... F is wrong because a null can't unbox to 0




With option F, as with option D, you are changing the total parameter from an Integer object, to a primative int. This should be fine, since autoboxing will do the conversion for you. This also doesn't have the problem as with option D as the null check has been removed... However, removing the check just makes it have a different error. Instead of a compile error with option D, this one will generate a runtime error, as null can still be returned, which doesn't unbox to zero.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

H is wrong because you
can't autobox a primitive just by trying to invoke a method with it.

what dose it mean that is wrong because ... H is wrong because you
can't autobox a primitive just by trying to invoke a method with it.?



Option H is probably the easiest to understand. The amount variable is a primative variable -- you can't dereference it to call a method. And autoboxing won't help you here.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hossam El-Gazzaz wrote:thank you henery for replying
the souce is s&k chapter 7

what about the other worng answers i'cant get them too?



I don't know if I actually help you here... all I did was take explanations, and used different words. What is so different about my explanations from the ones in the book?

Henry
 
Hossam El-Gazzaz
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think i have a problem we understanding boking it self

so thank you henery again
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hossam El-Gazzaz wrote:i think i have a problem we understanding boking it self

so thank you henery again



Well, it would be good if you can figure out what is it that is confusing you... Quite frankly, all I did was just explained with different words (with a little more elaboration).

If you can do that, maybe you can figure out the part of the subject that is confusing you -- so you can target your research.

Henry
 
reply
    Bookmark Topic Watch Topic
  • New Topic