code is
public class TestOverload {
public static void print(Float f, double d) {
System.out.println("Float,double");
}
public static void print(float f, double d) {
System.out.println("float,double");
}
public static void print(int f, double d) {
System.out.println("int,double");
}
// public static void print(int f, float d) {
// System.out.println("int,float");
// }
public static void print(double d1, double d) {
System.out.println("double,double");
}
public static void print(float d1, float d) {
System.out.println("float,float");
}
public static void main(String[] args) {
TestOverload.print(2, 3.0);
TestOverload.print(2, 3.0f);//Compiler error:The method print(float, double) is ambiguous for the type TestOverload
}
}
why it is giving error , instead it should pick print(float d1, float d)
PS:
in the above code,if i comment :
// public static void print(int f, double d) {
// System.out.println("int,double");
// }
then print(float d1, float d) is called…
Could be both
print(int, float)andprint(float, double)since implicit type conversions are done in the backgound. Anintcan be converted to afloat. Javac (or the compiler) cannot know for sure which one you meant.If you want to choose for your self you can add casts:
(Note that the second cast (float => float) isn’t necessary.)