Possible Duplicate:
Why does this get error?
The 3 methods below do exactly the same thing and obviously return true.
However, the first two compile but the third one does not ("missing return statement").
What part of the language specification dictates that behaviour?
boolean returnTrue_1() { // returns true
return true;
}
boolean returnTrue_2() { // returns true
for (int i = 0; ; i++) { return true; }
}
boolean returnTrue_3() { // "missing return statement"
for (int i = 0; i < 1; i++) { return true; }
}
If a method is declared to have a return type, then a compile-time error occurs if the body of the method can complete normally (§14.1).
see method body
and Normal and Abrupt Completion of Statements
Such a method must have a return statement that is garuanteed to be executed, which is not the case in v3.
There are cases where with human intelligence you know that it is garuanteed that return is called, but the compiler can not know it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With