wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Var-args and Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Var-args and Array" Watch "Var-args and Array" New topic
Author

Var-args and Array

Shaliey Gowtham
Ranch Hand

Joined: Mar 20, 2006
Posts: 104
Consider the two method signatures

Compiler complains about the method(int[] args) already defined for method(int...)
By removing the the code compiles fine.
On calling the method with

compiler error is produced informing method(int[]) can't be applied to
method(10,20);

If the two method declarations were same then the code should have compiled fine for method(10,20) invocation for method(int[]) but its generating error.
Also we are not permittedd to contain the both versions of the method.

We can then only create a int array and the invoke the method for the varargs parameter type.
Why this problem is arising?
Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
Because varargs is actually an array.

Try and you will see:

Shaliey Gowtham
Ranch Hand

Joined: Mar 20, 2006
Posts: 104
What if i need to have a method taking array as parameter,
and also need to invoke the varargs of the same type?
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Originally posted by Shaliey Gowtham:
What if i need to have a method taking array as parameter,
and also need to invoke the varargs of the same type?

There is no need to do what you are suggesting. Take the following example:

Suppose I declare this method:
Now:
The output you will experience should be:This is because the arguments to a var-arg method are all converted to a single array by the compiler, so when invoking output("Hello", myArr, "Last string"), in fact it is this following array which is passed to the method:So there should be no need to have both an array and var-args method of the same type because the var-args can handle the array as well as any 'loose' objects.


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Correction:Sorry, I meant to write:
i.e. the parameter "String... args" and not the "String args..." as I wrote before.

The edit facility annoyingly wouldn't let me edit the whole of my previous post!
Shaliey Gowtham
Ranch Hand

Joined: Mar 20, 2006
Posts: 104
Consider
Example 1:

Compiler complains that the overridden method doesn't have '...'
But when declared as
Example 2:

Still compiler complains that the method is already defined.

Is this the behavior expected?
[ May 10, 2006: Message edited by: Shaliey Gowtham ]
Harshil Mehta
Ranch Hand

Joined: Mar 17, 2005
Posts: 64
Shailey,
In case of

Compiler just gives a warning, else there is no compiling error encountered.
However I am more interested in the question you asked earlier,


What if i need to have a method taking array as parameter,
and also need to invoke the varargs of the same type?


Did you try the solution given by Charles,
would work for
.

I am not able to compile this code.
Are you able to compile it?
Edisandro Bessa
Ranch Hand

Joined: Jan 19, 2006
Posts: 584
Hi Shaliey,

For the exam just keep in mind that in fact var-args are internally treated as an array.

That's why the most known main method signature
public static void main(String[] args) can also be written as
public static void main(String... args) .

Both signatures are legal.

I recommend you to take a look at this thread, may be it can help you to understand it better.

After click the link please scroll down and take a look at my last two posts. The question I post there basically is the same you post here.


[ May 10, 2006: Message edited by: Edisandro Bessa ]

"If someone asks you to do something you don't know how to, don't tell I don't know, tell I can learn instead." - Myself
Shaliey Gowtham
Ranch Hand

Joined: Mar 20, 2006
Posts: 104
Harshil, i failed to notice the warning, thanks.
Thanks Edisandro, but its just to get about a little deep.
How to solve this:


How to solve this inheritance problem?
Is it overriding?
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
Originally posted by Harshil Mehta:
I am not able to compile this code.
Are you able to compile it?

Whoops, there goes me not thinking again... sorry I misread the 1.5 language guide first time around and thought it said the argument may be passed as an array or [inclusively] a sequence of arguments. I hadn't tried the previous code so I didn't realise it didn't work. In fact what it meant was:
The argument may be passed as an array or [exclusively] as a sequence of arguments.

So in fact what will work is trying to invoke my output() method with the following:

That will work okay - so you see there is no need for a separate output(String[]) method as the varargs one takes care of that as well. The output now will be:
Note that the lesson to be learnt from why my previous example doesn't work is that varargs are a compile time feature and not a runtime feature... what this means is that the signature of the vararg method does in fact have an array as its parameter (which is why declaring a second method with an identical array is illegal). So the compiler has to be able to determine at compile time either if the argument is an array (in which case it is passed directly) or if it is a sequence of arguments - but in this case the sequence is hard-coded into the source code, so the compiler can create a new array to hold the sequence and use that as the argument to the method.

Does that make sense now and resolve the issue(s)?
Shaliey Gowtham
Ranch Hand

Joined: Mar 20, 2006
Posts: 104
Charles,
What about the overriding feature and super class and child class relationship in these methods and about run time polymorphism?
Is this going out of the exam much?!!
[ May 10, 2006: Message edited by: Shaliey Gowtham ]
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
What about the overriding feature and super class and child class relationship in these methods and about run time polymorphism?

It is illegal to override an array method with a varargs one, or a varargs method with an array one. The compiler will complain about "overridden method has no ..." or "overriding method has no ..." respectively.

You therefore need to choose which type of parameter your method should have and stick with it. As I said, the varargs also takes the array, so in a sense varargs is more flexible.

Is this going out of the exam much?!!

I don't honestly know... varargs is an exam objective, so I suppose there could be a question on inheritence in there. Just remember: overriding is illegal between the two types. The overriding method must have exactly the same signature.

See also the Java Language Specification 3.0 under what they call "variable arity" parameters (posh name for varargs).

Oh, and by the way, you can declare the main method as either:
or
[ May 10, 2006: Message edited by: Charles Lyons ]
Shaliey Gowtham
Ranch Hand

Joined: Mar 20, 2006
Posts: 104
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Var-args and Array
 
Similar Threads
Boxing & Overloading with varargs
object array?
Constructor doubt
Doubt in Overloading
Method local inner class