Similar question about var-args in method signatures was asked few times (1, 2) but there is a corner case I don't get it. The compiler can distinguish between int...
and long...
overloaded method signatures and calls the method with smaller type.
However byte
and char
are clearly not the same size, yet the compiler complains that below test()
method is ambiguous:
static void test(byte... v) { System.out.println("Byte"); }
static void test(char... v) { System.out.println("Char"); }
public static void main(String[] args) {
test(); // Error:(7, 5) java: reference to test is ambiguous
// both method test(byte...) in App and method test(char...) in App match
}
Same happens for short...
and char...
however int...
and char...
are not ambiguous.
Why is char...
considered ambiguous with byte...
or short...
while by themselves byte...
and short...
are distinguishable in method signature?
static void test(byte... v) { System.out.println("Byte"); }
static void test(short... v) { System.out.println("Short"); }
public static void main(String[] args) {
test(); // Byte
}