I just saw someone use this alternate syntax for arrays and was wondering:
Is
public void fn(Character[]...){}
equivalent to
public void fn(Character[]){} //(i.e. "..." is redundant)
or is it equivalent to
public void fn(Character[][]){}
I just saw someone use this alternate syntax for arrays and was wondering:
Is
public void fn(Character[]...){}
equivalent to
public void fn(Character[]){} //(i.e. "..." is redundant)
or is it equivalent to
public void fn(Character[][]){}
Using varargs you will allow multiple parameters, using just a single 2D array you only allow one parameter. They might result in one 2D array, but they strongly differ in the way it's called.
Small sample:
public class Test {
public static void main(String[] args){
doSomething(new char[5], new char[6]);
doSomething(new char[2][3]);
}
private static void doSomething(char[]... args){
for(char[] s : args){
System.out.println(s.length);
}
}
}
Works just fine:
5
6
3
3
However if you would just use char[][]
, the first way of calling wouldn't work. That's the difference.
char
vs Char
makes no functional difference in this case, although you might want to keep the last remark from this answer in mind.