Variable Argumen List for a method

I

IchBin

There was a person learning Java and posted a question about an error
that he was getting in a program. The code was from a book he was
learning from. He did not mention this before hand. I did not test it
and said the you can not call a method with a variable argument list
for a method <below>.

To my amazement this variable argument list is legal. I know the the
For-loop came out in 1.5 but can not find anything about a variable
argument list for a method. I do not see it in the Java Language Specs
nor in the tutorials. When was this initially implemented in Java and
where is it documented.


printAll(2,"two",4,"Four",4.5,"Four Point Five");
printAll( );
printAll(25,"Anything Goes",4E4,false);

public static void printAll(Object ... args){ // Error Here
for(Object a:args){ // Error Here
System.out.println(a);

}

--
Thanks in Advance... http://ichbinquotations.awardspace.com
IchBin, Pocono Lake, Pa, USA http://ichbin.9999mb.com
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
H

Hemal Pandya

IchBin said:
There was a person learning Java and posted a question about an error
that he was getting in a program. The code was from a book he was
learning from. He did not mention this before hand. I did not test it
and said the you can not call a method with a variable argument list
for a method <below>.

To my amazement this variable argument list is legal. I know the the
For-loop came out in 1.5 but can not find anything about a variable
argument list for a method. I do not see it in the Java Language Specs
nor in the tutorials. When was this initially implemented in Java and
where is it documented.
[....]

The Varargs feature (aka methods with variable arity parameters) was
introduced with 1.5 as well. It is what makes the printf and other
similar methods work.
<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.1>

Also check how varargs methods affect overload resolution; in section
15.12.
 
I

IchBin

Hemal said:
IchBin said:
There was a person learning Java and posted a question about an error
that he was getting in a program. The code was from a book he was
learning from. He did not mention this before hand. I did not test it
and said the you can not call a method with a variable argument list
for a method <below>.

To my amazement this variable argument list is legal. I know the the
For-loop came out in 1.5 but can not find anything about a variable
argument list for a method. I do not see it in the Java Language Specs
nor in the tutorials. When was this initially implemented in Java and
where is it documented.
[....]

The Varargs feature (aka methods with variable arity parameters) was
introduced with 1.5 as well. It is what makes the printf and other
similar methods work.
<http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.1>

Also check how varargs methods affect overload resolution; in section
15.12.
Thanks for the info...

--
Thanks in Advance... http://ichbinquotations.awardspace.com
IchBin, Pocono Lake, Pa, USA http://ichbin.9999mb.com
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
J

John Ersatznom

IchBin said:
printAll(2,"two",4,"Four",4.5,"Four Point Five");
printAll( );
printAll(25,"Anything Goes",4E4,false);

public static void printAll(Object ... args){ // Error Here
for(Object a:args){ // Error Here
System.out.println(a);

}

It's legal Java 5. If it's failing to compile, you are probably:

a) using a 1.4 or earlier JDK or
b) using 1.4 or earlier source compatibility

Eclipse is a tricky bastard in this area -- it needs to be told to use
your current JDK *and* to use 1.5 source compatibility explicitly before
it will accept code like the above. This becomes especially hairy if you
have several JDK and JRE installs at various version levels, for testing
or whatever purposes. Make sure the JDK is aimed at the most recent (and
launch using the various JREs for testing purposes).
 
D

Daniel Dyer

It's legal Java 5. If it's failing to compile, you are probably:

a) using a 1.4 or earlier JDK or
b) using 1.4 or earlier source compatibility

It's worth noting that varargs in Java are somewhat retarded compared to
other languages. The problem is that they are not really compatible with
generics - a pretty poor show given that they were introduced at the same
time. The reason is that varargs are implemented as Object arrays and it
is not possible to create a generic array without a compile time warning.
To make it worse, suppressing the warning can't be done in one place, it
has to be done for every method invocation.

Dan.
 
I

IchBin

Daniel said:
It's worth noting that varargs in Java are somewhat retarded compared to
other languages. The problem is that they are not really compatible
with generics - a pretty poor show given that they were introduced at
the same time. The reason is that varargs are implemented as Object
arrays and it is not possible to create a generic array without a
compile time warning. To make it worse, suppressing the warning can't
be done in one place, it has to be done for every method invocation.

Dan.
No, I was surprised that it did compile. I somehow missed it for version
1.5.

--
Thanks in Advance... http://ichbinquotations.awardspace.com
IchBin, Pocono Lake, Pa, USA http://ichbin.9999mb.com
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
I

IchBin

John said:
It's legal Java 5. If it's failing to compile, you are probably:

a) using a 1.4 or earlier JDK or
b) using 1.4 or earlier source compatibility

Eclipse is a tricky bastard in this area -- it needs to be told to use
your current JDK *and* to use 1.5 source compatibility explicitly before
it will accept code like the above. This becomes especially hairy if you
have several JDK and JRE installs at various version levels, for testing
or whatever purposes. Make sure the JDK is aimed at the most recent (and
launch using the various JREs for testing purposes).

No, I was surprised that it did compile. I somehow missed it for version
1.5.

--
Thanks in Advance... http://ichbinquotations.awardspace.com
IchBin, Pocono Lake, Pa, USA http://ichbin.9999mb.com
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
I

IchBin

Daniel said:
It's worth noting that varargs in Java are somewhat retarded compared to
other languages. The problem is that they are not really compatible
with generics - a pretty poor show given that they were introduced at
the same time. The reason is that varargs are implemented as Object
arrays and it is not possible to create a generic array without a
compile time warning. To make it worse, suppressing the warning can't
be done in one place, it has to be done for every method invocation.

Dan.
Thanks Dan..

--
Thanks in Advance... http://ichbinquotations.awardspace.com
IchBin, Pocono Lake, Pa, USA http://ichbin.9999mb.com
______________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
T

Tor Iver Wilhelmsen

IchBin said:
No, I was surprised that it did compile. I somehow missed it for
version 1.5.

It's still just syntactic sugar for an Object[], though.
 
C

Chris Uppal

Tor said:
It's still just syntactic sugar for an Object[], though.

Not just Object[] arrays -- the type of the argument array is taken from the
type of the variadic method's formal parameters; it could be a String[] array
for instance.

-- chris
 
J

John Ersatznom

No, I was surprised that it did compile. I somehow missed it for version
1.5.

Funny it says "// Error Here" then. :) Regarding the Object[]
implementation, I don't suppose List<Object> ever occurred to them, and
I suppose List<Foo> for the general case someMethod(Foo ... args)? Heck,
the list could even be *modifiable*, the same way ordinary (non-final)
parameters are, with someMethod(final Foo ... args) making the list
reference "args" itself final in case some anonymous inner class needs it.
 
A

Alfred

IchBin said:
There was a person learning Java and posted a question about an error
that he was getting in a program. The code was from a book he was
learning from. He did not mention this before hand. I did not test it
and said the you can not call a method with a variable argument list for
a method <below>.

It's unnecessary. Use printAll(Collection); instead.

Alfred
 
A

Andreas Leitgeb

The problem is, that unlike "[]" (which can be before or after the
varname), the "..." *must* be afterwards.

Btw., internally (in the compiled class-file), a method declared with
vararg differs from a method with an array-argument only in method's
attribute "transient".
You can even define the "main" method with varargs, and have your
app started through it:
public static void main(String args...) { ... }

When a varargs method is called, the compiler generates code
to build up an array, and then calls the method with that array.
 
A

Andreas Leitgeb

Andreas Leitgeb said:
The problem is, that unlike "[]" (which can be before or after the
varname), the "..." *must* be afterwards.

Sorry for that goof. Of course the "..." must be after the type,
not after the varname!
(The position of "..." is more limited than that of "[]")

The rest appears to be goof-free even at second glance:
Btw., internally (in the compiled class-file), a method declared with
vararg differs from a method with an array-argument only in method's
attribute "transient".
You can even define the "main" method with varargs, and have your
app started through it:
public static void main(String... args) { ... }

When a varargs method is called, the compiler generates code
to build up an array, and then calls the method with that array.

This array cannot be changed for any other Collection, because
it also works for primitive types: e.g.: myMethod(int... x)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top