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

Question about boxing and args

 
Valmir Cinquini
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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
 
Nathan Warner
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic