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 Method overload doubt 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 "Method overload doubt" Watch "Method overload doubt" New topic
Author

Method overload doubt

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583


Why there is problem in below program to compile,why it can't have these two overloaded method together here



Tell the difficulties that i am difficult.
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

This is because, var args is nothing but an array of arguments.
e.g. 'int... args' is an array of int.
So, here, you are having two methods, with same name, same return type and same arguments - which is not method overloading, but it is duplicate method definition and hence the compile time error.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Because int[] and int... refer to the same type int[] and hence compiler gives an error. For overloading you should have the parameter list different.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Anayonkar Shivalkar wrote:This is because, var args is nothing but an array of arguments.
e.g. 'int... args' is an array of int.
So, here, you are having two methods, with same name, same return type and same arguments - which is not method overloading, but it is duplicate method definition and hence the compile time error.


Both are not same if they are then second program should work with same argument.



Output:- succeed

Now watch


Output:-compile error
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

The 'arguement-to-array' conversion takes place internally. The principle behind var-arg is that you can invoke the method with one or more arguments of a specific type, and within the method, all those arguments will be treated as an array.
Second code fails because you are passing a single int whereas method expects an int array.
First code works because the argument(s) are taken and further those are placed into an int array before passing to the method. You can further verify this by doing array specific operations in first code.
e.g. when you have 'int... args' as argument, and you invoke the method by passing a single int, then that int is assigned to arg[0], not to arg.
This is the reason why you cannot do int specific operations on doArgs in first code (e.g. doArgs++ will give you compile time error), and you can do array specific operations on doArgs there (e.g. printing doArgs.length etc.)
I hope this helps.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Answer
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1456
    
    5

It (almost)coincides with my and John Jai's statements
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method overload doubt
 
Similar Threads
var-args doubt clarification needed
array
Gotcha: Varargs spoofs method signature [from Sierra/Bates book]
DOUBT from scjp book
varargs doubt