I have a method with parameters (byte... a)
and have it overloaded with (long a, long b)
. Calling the method with two bytes casts it them to long
rather than using the provided (byte... a)
method with the correct type. I would expect it to use the method with the correct type rather than casting it. My question is what make Java choose long
over byte
for this ambiguous case?
public static void main(String[] args) {
byte b = 3;
foo(b);
foo(b,b);
foo(b,b,b);
}
static void foo(byte... a) {
for(byte elem:a) {
System.out.print(elem);
}
System.out.println();
}
static void foo(long a, long b) {
System.out.println("Java casts these as longs");
}
The output of this is
3
Java casts these as longs
333