I faced with same code:
public class Devk{
public static void tMeth(Integer... i){
System.out.print("A");
}
public static void tMeth(int... i){
System.out.print("B");
}
public static void main(String args[]){
tMeth(Integer.valueOf("7"));//compile error
tMeth(new int[2]); //returns B
tMeth(new Integer[2]); //returns A
}
}
after invokation I see
java: reference to tMeth is ambiguous, both method tMeth(java.lang.Integer...) in GenericsTest.Test1 and method tMeth(int...) in GenericsTest.Test1 match
method Integer.valueOf("7")
returns Integer wrapper. I expect to see A
in console.
Who can explain this behaviour and provide general rule for this ?
P.S.
public static void tMeth(Integer i){
System.out.print("A");
}
public static void tMeth(int i){
System.out.print("B");
}
public static void main(String args[]){
tMeth(1); //returns B
tMeth(new Integer(1)); //returns A
}