• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

OCA Java SE 8 Programmer I, Sybex Page 250, Is this errata?

 
John Pacuta
Greenhorn
Posts: 26
1
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The following code is taken from Sybex page 250 (some methods are omitted). Specifically, why would Reptile.hasLegs() compile? It doesn't have a return type! Am I missing something?



 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Pacuta wrote:Specifically, why would Reptile.hasLegs() compile? It doesn't have a return type! Am I missing something?


From the method declaration, it looks like the method is supposed to return a Boolean object... or do you mean the code body itself?

Henry
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Hope it helps!
Kind regards,
Roel
 
Ganesh Patekar
Bartender
Posts: 694
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

  •  
    Ganesh Patekar
    Bartender
    Posts: 694
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
  • As Roel De Nijs mentioned, try adding return statement after or before throw new InsufficientDataException(); like this.
  • OR
  • 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
  •  
    Jeanne Boyarsky
    author & internet detective
    Marshal
    Posts: 35279
    384
    Eclipse IDE Java VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Great explanation Ganesh!
     
    Ganesh Patekar
    Bartender
    Posts: 694
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Jeanne
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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:
  • return value for method.
  • Confused point about infinite loop
  • continue and break in a loop would generate compilation error?
  • Which of these is unreachable code?
  • Unreachable Code
  • Which is the first line to cause error?
  • Maybe "unreachable code" should be "dead code" in page 70, (Java OCA 8 Programmer I Study Guide)
  • Throwing a second exception question
  • System.exit() and unreachable code
  • good examples of unreachable code?

  • I think you know what to do this evening

    Hope it helps!
    Kind regards,
    Roel
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.

    Hope it helps!
    Kind regards,
    Roel
     
    John Pacuta
    Greenhorn
    Posts: 26
    1
    Android Java Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks to Ganesh for explaining abnormal method completion. Great answers all around (especially the nitpicking, the compiler knows no mercy).
     
    Ganesh Patekar
    Bartender
    Posts: 694
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    My pleasure ,  Don't hesitate to go through the links provided by Roel De Nijs, those were good enough to make me believe in, that I'm yet to learn much more
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic