Testing a variant of your code, except with a byte
literal and overloaded methods with various combinations of short
, int
, and long
appears to imply that the compiler chooses the "least widening" conversion if more than one is available.
Thus:
- Between a
short
and an int
, if you call the overloaded method with a byte
, the short
variant will be chosen
- Between an
int
and a long
, if you call the overloaded method with a byte
or short
, the int
variant will be chosen
And so forth.
Thus, because long
can be widened to either float
or double
, and because the float
conversion is the "least widening", the float
overload is chosen.
I think this is because of the "choose the most specific overload" way that the compiler resolves multiple possible overloads. From the JLS, section 15.12.2.5:
The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time error.
So by this, a method that takes a float
is "more specific" than a method that takes a double
because any invocation handled by a method that takes a float
can always be handled by a method that takes a double
, but not the other way around.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…