wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about boxing and args Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about boxing and args" Watch "Question about boxing and args" New topic
Author

Question about boxing and args

Valmir Cinquini
Greenhorn

Joined: Nov 11, 2009
Posts: 26
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

Joined: Nov 16, 2009
Posts: 4
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

Joined: Nov 11, 2009
Posts: 26
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
 
 
subject: Question about boxing and args