Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes boxing vs var-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 "boxing vs var-args" Watch "boxing vs var-args" New topic

boxing vs var-args

Beno Zehra

Joined: Jun 10, 2008
Posts: 1
Hi all,

I am following K&B book for SCJP preparation. In page 240 of K&B, it
is clearly written that Boxing beats var-args, but on same page, there
is one line written like this

"A good way to remember this rule is to notice that the var-args
method is looser than the other method, in that it could handle
invocations with any number of int parameters. A var-arg method
is more like a catch-all method in terms of what invocations it
can handle."

What does this statement exactly means.


Beenish<br />Moderator:<br /><a href="" target="_blank" rel="nofollow"></a>
Dariusz Kordonski
Ranch Hand

Joined: Jul 11, 2008
Posts: 49
This means that var-arg methods are more general, because they can be matched to more number of invocations than no-var-arg methods.

This is some analogy to Exceptions (I'm using this example, because of the 'catch-all' statement from the citation you provided). Statment 'catch(Exception e)' will match more exceptions than 'catch(RuntimeException re)'. There is a kind of (general rule for such cases in Java - the more specific pattern is matched first. In case of exceptions for instance you declare catch(RuntimeException re) before catch(Exception e) (within one catch block) - otherwise the compiler will complain, bacause it knows that the catch(Exception e) will 'catch all' exceptions, so catch(RuntimeException re) would never be reached. This way some more specific type of exceptions are handled in a more 'tailored' manner, and all the rest (non-RuntimeException) is taken care of by the more general catch clause ('catch(Exception)'). By analogy in var-arg vs. no-var-arg methods: if no-var-arg method (more specific) can be matched by a given inocation, the compiler will prefer it over a var-arg one.
I hope this helps. Regards
I agree. Here's the link:
subject: boxing vs var-args
It's not a secret anymore!