Reputation: 5654
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...
Upvotes: 1
Views: 224
Reputation: 43504
print(2, 3.0f);
Could be both print(int, float)
and print(float, double)
since implicit type conversions are done in the backgound. An int
can be converted to a float
. Javac (or the compiler) cannot know for sure which one you meant.
If you want to choose for your self you can add casts:
print((float) 2, (float) 3.0f);
(Note that the second cast (float => float) isn't necessary.)
Upvotes: 4