Java overload confusion

后端 未结 4 1427
广开言路
广开言路 2020-12-19 14:13

java is not able to call any overload method as shown below :-

class LspTest{

    public void add(int a, float b){
    System.out.println(\"First add\");
}         


        
4条回答
  •  失恋的感觉
    2020-12-19 14:36

    When you initialise with literal values, in this case, compiler won't be able to infer the exact type. Therefore, it does not know which overload to call and returns the error that the reference to add is ambiguous. You can fix this by casting the arguments to the appropriate type, or even better, creating typed local variables initialised with 1 and passing the variables as parameters, like so:

    int a = 1;
    float b = 1;
    LspTest test = new LspTest();
    test.add(a,b);
    

提交回复
热议问题