Java overload confusion

后端 未结 4 1434
广开言路
广开言路 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-19 14:56

    This is the clear case of ambiguity which leads to a Compile Error.

    Java compiler supports the type promotion. First of all, it'll checks for more specific data type if not match then it'll promote to next data type.

    Java compiler will supports the type promotion in following order.

    byte --> short --> int --> long --> float --> double

    As your parameters (int,int) can be auto-promoted to float, java compiler can't decide in which one to invoke as both of your methods accepts the (1,1)

提交回复
热议问题