Why does byte not get type promoted to long (Type promotion during method overloading)

90 views Asked by At

In the first piece of code byte gets type promoted to integer type

public class Main
{
    Main(int a,byte b){
        System.out.println("int 1");
    }
    
    Main(int a,float b){
        System.out.println("float 1");
    }
    
    Main(float a,int b){
        System.out.println("float 2");
    }
    
    public static void main (String[] args) {
        byte a = 10;
        byte b = 20;
        Main obj = new Main(a,b);
    }
}

Output
int 1

In the second code the compiler throws an error

public class Main
{
    Main(long a,byte b){
        System.out.println("int 1");
    }
    
    Main(int a,float b){
        System.out.println("float 1");
    }
    
    Main(float a,int b){
        System.out.println("float 2");
    }
    
    public static void main (String[] args) {
        byte a = 10;
        byte b = 20;
        Main obj = new Main(a,b);
    }
}

Error

Main.java:25: error: reference to Main is ambiguous
        Main obj = new Main(a,b);
                   ^
  both constructor Main(int,float) in Main and constructor Main(float,int) in Main match
1 error


** Process exited - Return Code: 1 **

Why in the second code the constructor with long a and byte b as parameters is not being called?

0

There are 0 answers