Java - What is the output: overloaded methods and automatic type widening

Question

What is the output from the following code

class Adder {
  public double add(int a, double b) {
    return a + b;
  }

  public double add(double a, int b) {
    return a + b;
  }
}

public class Main {
  public static void main(String[] args) {
    Adder a = new Adder();
    double d = a.add(2, 3);
    System.out.println(d);
  }
}


Click to view the answer

double d = a.add(2, 3); // A compile-time error

Note

The error message states that compiler is not able to decide which one of the two add() methods in the Adder class to call for a.add(3, 7) method invocation.

To fix the error

double d1 = a.add((double)2, 3); // OK. Will use add(double, int)
double d2 = a.add(2, (double)3); // OK. Will use add(int, double)