• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why not "Unreachable Code" Error..?

 
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why this code is not throwing "Unreachable Code" compile time error at line no 21.

If the conditional statements are not evaluated by compiler then whats happening in the code below: why it is giving compiler error at line no 7 while I was expecting null4..

Please let me know some concrete rule to handle such problems...
 
Saloon Keeper
Posts: 15522
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since the compiler doesn't evaluate the conditional statement, it won't determine that you always return null. As far as it's concerned, it might terminate the if statement without executing the return statement, so you still need to return some value below the if statement.
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello stephan,
thanks for your reply!
Got your point to some extent. Will you please explain your answer in terms of examples i have given here..?

Stephan van Hulst wrote:Since the compiler doesn't evaluate the conditional statement, it won't determine that you always return null. As far as it's concerned, it might terminate the if statement without executing the return statement, so you still need to return some value below the if statement.

 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swaraj gupta wrote:
Got your point to some extent. Will you please explain your answer in terms of examples i have given here..?


The compiler doesn't check the condition, but checks the syntax. And, the compiler checks, whether in all condition, you've return a String from that method. Did you returned in all ways that compiler expects?
 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see this.
 
swaraj gupta
Ranch Hand
Posts: 186
Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You mean to say, method should not fail to return String in any case here. And as in this case as per the compilers point of view it is possible that conditional statement might get false (as it checks the syntax only) and then the method will not return any thing, thats why it is giving compile time error. Am I right ?

Abimaran Kugathasan wrote:

swaraj gupta wrote:
Got your point to some extent. Will you please explain your answer in terms of examples i have given here..?


The compiler doesn't check the condition, but checks the syntax. And, the compiler checks, whether in all condition, you've return a String from that method. Did you returned in all ways that compiler expects?

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

swaraj gupta wrote:
You mean to say, method should not fail to return String in any case here. And as in this case as per the compilers point of view it is possible that conditional statement might get false (as it checks the syntax only) and then the method will not return any thing, thats why it is giving compile time error. Am I right ?



Yes, because compiler don't know logics.
reply
    Bookmark Topic Watch Topic
  • New Topic