• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

boxing vs var-args

 
Beno Zehra
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Ben
 
Dariusz Kordonski
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic