String[] args fights String... args

Hi,

I thought this error message was a joke at first, but it's for real:

Arrayz.java:15: cannot declare both in(java.lang.String,java.lang.String[]) and in(java.lang.String,java.lang.String...) in krc.utilz.Arrayz

Please, would anyone care to offer an opinion as to WHY the compiler does not differentiate betweenString[] args andString... args. ObviouslyString... args is implementedinternally asString[] args, but that really shouldn't effect the interface... and if they are internally equivalent then wny not allow a syntax that effectively declares both in one... It's just that I would have thought it was possible (and desirable) for the compiler & JVM to do the required parameter matching for both definitions... in my mind the calling parameter list of (String, String, String) is distinct from (String, String[]) ... Or does java (like C/C++) internally not differentiate between the types reference-to-String and reference-to-array-of-Strings and reference-to-array-of-array-of-Strings ?

I'm just a tad miffed about this... I mean, it's easy to work around in at least a dozen simple ways, it's just that the compiler didn't do what I expected it to do, so it should just pull it's socks up, and, well, you know, Work!

Here's the code... complete with compiler error.

package krc.utilz.stringz;

publicclass Arrayz

{

/**

* returns true if the given value is in the args list, else false.

* @param value - the value to seek

* @param args... - variable number of String arguments to look in

*/

publicstaticboolean in(String value, String... args){

for(String a : args)if(value.equals(a))returntrue;

returnfalse;

}

//Arrayz.java:15: cannot declare both in(java.lang.String,java.lang.String[]) and in(java.lang.String,java.lang.String...) in krc.utilz.Arrayz

publicstaticboolean in(String value, String[] array){

for(String s : array)if(value.equals(s))returntrue;

returnfalse;

}

}

Thanx all. Keith.

[3042 byte] By [corlettka] at [2007-11-27 7:09:49]
# 1

I think it's because you can also pass an array reference to the varargs parameter. So both calls work with one definition:

public static boolean in(String value, String... args);

..

in("Some string", new String[]{"sdf", "asd"}]);

in("Some string", "sdf", "asd");

-Puce

Pucea at 2007-7-12 19:01:15 > top of Java-index,Java Essentials,New To Java...
# 2
> Please, would anyone care to offer an opinion as to> WHY the compiler does not differentiate between> String[] args and String... args.Because they are the same thing by definition.
jsalonena at 2007-7-12 19:01:15 > top of Java-index,Java Essentials,New To Java...
# 3

I didn't know that. Cool!

package forums;

class VarArgsTester

{

public static String join(String FS, String... args) {

if (args==null) return null;

if (args.length==0) return "";

StringBuffer sb = new StringBuffer(args[0]);

for (int i=1; i<args.length; i++) {

sb.append(FS+args[i]);

}

return sb.toString();

}

public static void main(String[] args) {

System.out.println(join(" ", "Your", "momma", "wears", "army", "boots"));

String[] words = {"But", "she", "looks", "very", "nice", "in", "them"};

System.out.println(join(" ", words));

}

}

I was expecting a compiler error from the line[

pre]System.out.println(join(" ", words));

Thanks ~Puce.>

corlettka at 2007-7-12 19:01:15 > top of Java-index,Java Essentials,New To Java...
# 4
http://java.sun.com/docs/books/jls/third_edition/html/classes.html#38698~
yawmarka at 2007-7-12 19:01:15 > top of Java-index,Java Essentials,New To Java...