Because you are not required to capture the return type 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?
Because you are not required to capture the return type 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?
2 oh duh...don't know why I didn't realize that – llm Apr 30 '10 at 12:40 4 For the sake of clarity, perhaps you should change the void return type to something else (like boolean). I originally looked at this example and thought " the method whose return type is void should be run because no return value is being captured! ".
:) – Adam Paynter Apr 30 '10 at 12:56 1 @Adam, good point, changed. Thanx :-) – Péter Török Apr 30 '10 at 13:01 Interestingly, the Java language forbids this, but the JVM does not. See my answer... – Lukas Eder Apr 30 '10 at 22:27.
I think you may find solution in below link. stackoverflow.com/questions/442026/funct....
3 +1, It's really worth reading, it gives much more insight than just stating that the compiler can't resolve it. – Tomas Vana Apr 30 '10 at 13:00 1 +1 - as Thomas say, the answer to the linked question is very informative; it also points out, interestingly, that the JVM does in fact allow overloading by return type, even if Java-the-language doesn't. – Cumbayah Jun 16 '10 at 18:27.
It's because you are free to ignore return value.
I've wondered why they don't support this also. Sure, if you ignore the return value, the compiler would have no way to know which you wanted. But that's the same ambiguity that arises with passing nulls.
Like: String doSomething(String s) { ... } String doSomething(Integer s) { ... } ... String out=doSomething(null); In this case, the compiler just complains that the call is ambiguous, and you have to resolve it by casting the null, like: String out=doSomething((String)null); You could do the same thing with overloading by return type: String getSomething() { ... } Integer getSomething() { ... } ... Integer n=getSomething(); would presumably call the second function. GetSomething(); would be ambiguous (and in this example, probably useless, unless it had side effects, but that's another story), so you'd have to say: (String) getSomething(); More realistically, perhaps: if ((String) getSomething()==null) ... But that's the easy case. I can see a compiler-writer not wanting to support this because it could get very complicated to figure out in anything other than a simple assignment.
For example, consider: String getSomething() { ... }; Integer getSomething() { ... }; String getOtherthing() { ... }; ... if (getSomething(). Equals(getOtherthing())) ... The compiler would have to figure out that both String and Integer have equals functions, so either one is valid at that point. Then it would have to notice that getOtherthing is a String, and Integer.
Equals(String) is unlikely, so probably what the writer wanted was String. Equals(String). Do-able, but at that point I'm starting to see that in the general case, this could be a beast.
And then suppose we add: Integer getOtherthing() { ... }; Now what does the compiler do with that IF statement? It could use the String versions of both functions, or the Integer, but not the String of one and the Integer of the other. At that point it would have to insist on a cast to tell it which, I guess.
But the complexity is really getting out of hand. And if it's hard for the compiler to figure out what you really mean, imagine what it would be like for another programmer who can't look up all the function signatures as fast as the compiler can.
1 Very good insight! – Adam Paynter Apr 30 '10 at 14:50.
While it is theoretically possible, it was not used in Java for the same reason it wasn't used in C++; namely, it has been found that overloads based on return-types are generally more confusing to developers, the benefit is marginal compared with the costs of implementing it, and it would be ambiguous in the case where the return-type is not assigned to a value. For those reasons return-type based overloading is not supported.
Confusing to developers AND compilers, especially if the call isn't used in a context where a return value is required. – justkt Apr 30 '10 at 12:50.
I think one of the reasons is that, in most case you can determine the return type of a function only after the execution of the function instead of before this process. Thus, it can not help you decide which overloaded function to invoke just based on different return types of functions.
This is a good reason in a dynamic language, however in Java the method signature to call (including return type) is resolved at compile time, so since the return type is understood by the compiler, it could theoretically resolve this (if not for the issue in the other answers). – Yishai Apr 30 '10 at 14:27 @Yishai: thanx. :) – Summer_More_More_Tea Apr 30 '10 at 16:42.
One interesting aspect about this question is the fact that the Java language forbids overloading methods only by return type. But not the JVM: Note that there may be more than one matching method in a class because while the Java language forbids a class to declare multiple methods with the same signature but different return types, the Java virtual machine does not. This increased flexibility in the virtual machine can be used to implement various language features.
For example, covariant returns can be implemented with bridge methods; the bridge method and the method being overridden would have the same signature but different return types. From: Class. GetMethod(String, Class...).
1 this was new to me, thanks :-) – Péter Török Jun 20 at 14:08 Yes, the depths of the JVM. Also, with generic method parameters there are some very nice JVM tricks that apply using "synthetic" methods, which don't exist in the Java language, either... – Lukas Eder Jun 20 at 14:15.
Because Java can cast the return type value to the target variable, so it doesn't know into what variable type are you storing the return value of the function.
Sure, if you ignore the return value, the compiler would have no way to know which you wanted. But that's the same ambiguity that arises with passing nulls. Would presumably call the second function.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.