Ambiguty and no suitable method found error overloading Java

Suppose I have one overloaded class as shown below

class Test{
    public void m1(int a,float b){
        System.out.println("hello");
    }
    public void m1(float a,int b){
        System.out.println("hai"); 
    }
    public static void main(String[] args){
        Test t = new Test();
        t.m1(10,10);//error
        t.m1(10.5f,10.6f);//error
    }
}

when I call a method m1()with two int values, such as m1(10,10), error

 error: reference to m1 is ambiguous, both method m1(int,float) in Test and method m1(float,int) in Test match
t.m1(10,10);
 ^

and when I call the method m1()with two floating point values ​​like m1(10.5f,10.6f)error

error: no suitable method found for m1(float,float)
t.m1(10.5f,10.6f);
 ^
method Test.m1(float,int) is not applicable
  (actual argument float cannot be converted to int by method invocation conversion)
method Test.m1(int,float) is not applicable
  (actual argument float cannot be converted to int by method invocation conversion)

Can someone explain the reason why this program shows two different types of errors?

+4
source share
1 answer

int , float, int float ( ). t.m1(10,10), ( int float, ). , reference to m1 is ambiguous.

float , int, int int, float int. t.m1(10.5f,10.6f).

+5

All Articles