I'm a little confused about Java's varargs methods:
public static int sum(int ...a) {
return 0;
}
public static double sum(double ...a) {
return 0.0;
}
When I tried to invoke sum()
without passing any argument, then the int
version of method was invoked. I don't understand why; normally the compiler must raise an error.
By contrast, the following piece of code generates a compiler error when I try to invoke sum
without any argument:
public static int sum(int ...a) {
return 0;
}
public static boolean sum(boolean ...a) {
return true;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…