Java Forum / General / January 2007
Variable Argumen List for a method
IchBin - 03 Jan 2007 04:29 GMT 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);
}
 Signature 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-)
Hemal Pandya - 03 Jan 2007 06:09 GMT > 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 [quoted text clipped - 7 lines] > 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.
IchBin - 03 Jan 2007 08:21 GMT Hemal Pandya wrote:
>> 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 [quoted text clipped - 16 lines] > Also check how varargs methods affect overload resolution; in section > 15.12. Thanks for the info...
 Signature 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-)
John Ersatznom - 04 Jan 2007 13:13 GMT > printAll(2,"two",4,"Four",4.5,"Four Point Five"); > printAll( ); [quoted text clipped - 5 lines] > > } 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).
Daniel Dyer - 04 Jan 2007 13:21 GMT >> printAll(2,"two",4,"Four",4.5,"Four Point Five"); >> printAll( ); [quoted text clipped - 8 lines] > 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.
 Signature Daniel Dyer http://www.uncommons.org
IchBin - 04 Jan 2007 13:45 GMT >>> printAll(2,"two",4,"Four",4.5,"Four Point Five"); >>> printAll( ); [quoted text clipped - 18 lines] > > Dan. No, I was surprised that it did compile. I somehow missed it for version 1.5.
 Signature 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-)
John Ersatznom - 06 Jan 2007 03:09 GMT >>>> public static void printAll(Object ... args){ // Error Here
> 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.
IchBin - 04 Jan 2007 13:47 GMT >>> printAll(2,"two",4,"Four",4.5,"Four Point Five"); >>> printAll( ); [quoted text clipped - 18 lines] > > Dan. Thanks Dan..
 Signature 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-)
IchBin - 04 Jan 2007 13:46 GMT >> printAll(2,"two",4,"Four",4.5,"Four Point Five"); >> printAll( ); [quoted text clipped - 17 lines] > 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.
 Signature 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-)
Tor Iver Wilhelmsen - 04 Jan 2007 16:19 GMT > 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.
Chris Uppal - 04 Jan 2007 19:36 GMT > 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
Andreas Leitgeb - 06 Jan 2007 16:10 GMT >> public static void printAll(Object ... args){ // Error Here 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.
Andreas Leitgeb - 06 Jan 2007 16:21 GMT >>> public static void printAll(Object ... args){ // Error Here > 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 [quoted text clipped - 5 lines] > 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)
Alfred - 06 Jan 2007 09:27 GMT > 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
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|