6
in the example below
public static void foo(Integer i) {
System.out.println("foo(Integer)");
}
public static void foo(short i) {
System.out.println("foo(short)");
}
public static void foo(long i) {
System.out.println("foo(long)");
}
public static void foo(int... i) {
System.out.println("foo(int ...)");
}
public static void main(String[] args) {
foo(10);
}
foo calls long signature method, because JVM resolves that the closest type of int is long, which is understandable.
he prefers to do this rather than calling the method with Integer signature or int... (array), which would be perfectly compatible.
what is the reason, in terms of language design, for "long" to be more suitable than Integer or int...? Is it expensive for JVM to do this kind of casting internally? (either for int[] or Integer)
+1 compatibility. great point, thanks
– Leo