Java - why no return type based method overloading?

llm picture llm · Apr 30, 2010 · Viewed 20.9k times · Source

I know this is not possible but can anyone provide a theory as to why Java chose not to support this? I am asking because I just ran into a situation where I think it would be nice to have.

Answer

Péter Török picture Péter Török · Apr 30, 2010

Because you are not required to capture the return value of a method in Java, in which case the compiler can not decide which overload to use. E.g.

boolean doSomething() { ... }

int doSomething() { ... }

doSomething(); // which one to call???