• 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

Question about boxing and args

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the following code:



Why the answer is 212 ??? Why it is not 232???

Why the second call (doStuff( x );) falls in doStuff(Object o) and not doStuff(long L) ? Both x and L are primitives. An int doesn't fit in a long?

Can anyone help me with boxing and args? This subject isn't clear for me.

Thanks in advance
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The reason goes for the Object overload instead of the Integer var-arg is to allow older code to keep the same functionality even with the introduction of var-args for method calls. This means that using a var-arg argument for a method is the lowest priority. What it does is it boxes the int to and Integer object and promotes it to its Object superclass.

I just ran the same code and the output that I got was 242 instead of 212. This is what the output should be because widening the int to a long, which is done implicitly, has a higher priority to boxing it to and Integer and then widening it to its Object superclass.
 
Valmir Cinquini
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nathan Warner wrote: I just ran the same code and the output that I got was 242 instead of 212. This is what the output should be because widening the int to a long, which is done implicitly, has a higher priority to boxing it to and Integer and then widening it to its Object superclass.



Hi Nathan,

thanks for your reply. I also ran the code and I got 242. I think the book was mistyped.

Thanks anyway
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic