File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overloading in Combination with 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 "Overloading in Combination with Var-args" Watch "Overloading in Combination with Var-args" New topic
Author

Overloading in Combination with Var-args

kiruthigha rajan
Ranch Hand

Joined: Dec 29, 2011
Posts: 69
Hai can anyone explain me how does the code work??
im unable to understand!!!
thanks in advance

class Vararg {
static void wide_vararg(long... x)
{ System.out.println("long..."); }
static void box_vararg(Integer... x)
{ System.out.println("Integer..."); }
public static void main(String [] args) {
int i = 5;
wide_vararg(i,i); // needs to widen and use var-args
box_vararg(i,i); // needs to box and use var-args
}
}
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1174
Hi Kiruthigha,

Can you please be more specific about your question?

Regards,
Dan

William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
ujjawal rohra
Ranch Hand

Joined: Mar 20, 2010
Posts: 105
When calling a method if there is no method matching the exact signature of the called method then the arguments are widened or boxed as required.

The method wide_vararg(long... x) takes a var-arg of type long, that means it can take one or more than one long arguments.
Now when this method is invoked using wide_vararg(i,i) having two ints, then the two ints are matched due to the var-args in the declaration of the method and also the ints are widened and promoted to longs. In this way this method matches the call and is called.

Similarly, when calling box_vararg(i,i), the arguments(two ints) are matched due to var-args declaration and then they are boxed due to an Integer in the declaration.

Hope this helps..

SCJP 6
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overloading in Combination with Var-args