John Pacuta wrote:Specifically, why would Reptile.hasLegs() compile? It doesn't have a return type! Am I missing something?
Yes, you definitely are!
First some nitpicking: you have to be more careful about your wordings: the hasLegs() method of the Reptile class definitely has a return type. It's Boolean! But this method has indeed no return value.
Secondly would a return value be useful in this case? Think about it: on each invocation this method will throw a InsufficientDataException. So returning a value makes no sense at all! You always get the exception.
In fact, if you try to add a return statement (e.g. return false;) after (or before) the throw statement, the compiler will complain and the code will fail to compile with an "unreachable code" compiler error.
JLS 8 14.1. wrote:The break (§14.15), continue (§14.16), and return (§14.17) statements cause a transfer of control that may prevent normal completion of statements that contain them.
JLS 8 14.17 wrote:A return statement returns control to the invoker of a method (§8.4, §15.12) or constructor (§8.8, §15.9).
Means when a method has non-void return type or we can say it returns something, in this case returns Boolean casues a transfer of control to the invoker of method which prevents normal completion of that method, in this case hasLegs().
A return statement always completes abruptly.
Example 1: Abnormal completion of method hasLegs()
In below code the return statement completes abruptly, the reason being a return with value true.
Here you must return boolean value to makes abnormal completion of this method then it compiles successfully.
JLS 8 8.4.7 wrote:If a method is declared to have a return type (§8.4.5), then a compile-time error occurs if the body of the method can complete normally (§14.1).
Example 2: Normal completion of method hasLegs()
In below code method body completes normally because there is no return to abrupt this method, so gives compile time error which says This method must return a result of type Boolean.
Why it says that? because compiler wants you to make abnormal completion of the body of this method.
A method with a return type must return only by using a return statement that provides a value return.
The method is not allowed to "drop off the end of its body" means control should not reach the end of the body i.e. } closing curly brace of this method.
Example 3: Abnormal completion of method hasLegs()
It is possible for a method to have a return type and yet contain no return statements. Here is below example.
Abnormal completion of the body of method hasLegs in below example, not because of return but because of throwing control to user defined exception i.e. InsufficientDataException
In below example body of method is not completed normally because of that exception which satisfies JLS rule of return, so no compile time error.
As Roel De Nijs mentioned, try adding return statement after or before throw new InsufficientDataException(); like this.
Which gives CE saying Unreachable code because compiler is so smart, it knows that
In first example If this method is called then control will sure transfer to InsufficientDataException then return statement will not get executed so gives CE.
And in second example If this method is called then it will sure transfer control to the invoker of this method using return statement then throw new InsufficientDataException(); will never get executed so gives CE
Ganesh Patekar wrote:Which gives CE saying Unreachable code because compiler is so smart, it knows that
* In first example If this method is called then control will sure transfer to InsufficientDataException then return statement will not get executed so gives CE.
* And in second example If this method is called then it will sure transfer control to the invoker of this method using return statement then throw new InsufficientDataException(); will never get executed so gives CE
The "unreachable code" topic is one of the more popular ones in this forum. So using the search function you'll find plenty of topics about "unreachable code". Here is a list with a few of these topics with excellent explanations and illustrative code snippets:
John Pacuta wrote:The following code is taken from Sybex page 250 (some methods are omitted).
And here is some more nitpicking In the study guide on page 250 the return type of the hasLegs() method is boolean (and not Boolean). Although for your question it is not important, there is a significant difference between boolean and Boolean: the first is a primitive data type and the latter is a primitive wrapper class. Thanks to autoboxing (which was added in Java 5) you can use a boolean literal (true or false) when a Boolean is expected, like in this hasLegs() methodIf you would compile this in Java 4 (and prior versions), you'll get a compiler error.
Another difference: a boolean (primitive) variable can only have two values: true or false; but for a Boolean (reference) variable: Boolean.TRUE, Boolean.FALSE, and null.