Search code examples
javaoverloadingsignature

Is it possible to specify which overloaded function runs in Java


Given the methods:

String[] foo(int i){#doSomething;}

String[] foo(double i){#doesSomethingDifferent;}

Is it possible to write foo(int i){return foo(double d);} so that it does not run the recursive foo(int i){return foo(int i);} ?

While I understand this would would not be recommended by any means, I am searching to find what mechanisms would allow or disallow it?


Solution

  • You specify implicitly the method that will be called by passing an actual parameter.

    foo(int i) { return foo(1d); /*it will call foo(double i)*/ }
    foo(int i) { return foo(1); /*recursive call*/ }