• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

how compiler detects unreachable code and what are compile time constants?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the below code i am getting "unreachable code" error at line 09, at compile time. How the compiler detects that it is an unreachable code?



While for below code it does not detect "unreachable code".


and what are "compile time constants"?
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

The compiler is smart enough to detect that in the first example the only way out of the loop is the break outer statement, which makes the System.out (since we are in a while(true) loop) unreachable.

As for the second example, the compiler is smart enough to know that the break statement will never be reached due to the if(false).

There is already a discussion about compile time constants, check this thread https://coderanch.com/t/403358/Beginning-Java/java/Compile-time-constant
 
Ranch Hand
Posts: 814
Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ash,
Line number 11 is unreachable.
See this for details.
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In this code there will be an unreachable code compiler error as "last" won't be reached if something happens and the if loop runs. Well unreachable statements are interesting and here we have to see the compiler's point of view. Compiler sees that the break is reachable but if it breaks a loop always which has some statements then those statements won't be reached.
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ash aj wrote:
and what are "compile time constants"?



I wrote a detailed post on this last month. Please let me know what you think.

https://coderanch.com/t/454384/Beginning-Java/java/What-compile-time-constant

Henry
 
Shivani Yadav
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler does not give "unreachable" compiler error for the second code fragment i.e.


even though the code in if block and the sysout will never get executed. Why the complier is not able to detect this? What are the rules through which compiler detects the unreachable code?
 
Henry Wong
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ash aj wrote:The compiler does not give "unreachable" compiler error for the second code fragment i.e.

even though the code in if block and the sysout will never get executed. Why the complier is not able to detect this? What are the rules through which compiler detects the unreachable code?



Take a look at the link provided by Ninad again (second response).

A special rule applies for "if" statements -- basically, if the "if" statement is reachable, then it will be assumed that the code can complete. Meaning that the block inside the statement will run, regardless of the fact that the condition is always false.

This rule is needed in place of conditionally compile code, such as those needed for debugging statements.

So... since, the "if" can complete, the "break" is reachable, and hence, the code after the "while" loop is reachable.

Henry
 
Bartender
Posts: 2856
10
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"ash aj " please check your private messages. You can check them by clicking the My Private Messages link above.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic