• 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

Unreachable Code

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When can the compiler know that a code is unreachable?


In the above code compiler does not give unreachable code erro. but still it can decide it at compile time. for example in the following code, compiler knows that variable i is initialzed and it will not give any compile time error

why doesn't compiler throw unreachable code error in the first case?
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's the if condition which prevents the compiler from reporting a Unreachable code error. The if construct involves a conditional check which can result in both a true value or a false value. Since the return becomes conditional, compiler doesn't report the error. I think it's not the compiler's job to see that the condition provided by the developer will never be false.
The following code, however, will report Unreachable code error. An attempt to call this method will result in java.lang.Error: Unresolved compilation problem: Unreachable code


Thanks,
Abhinay
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai ravi,

-------------------------------
void method1()
{
if (true)
return;
System.out.println("print");
}
-------------------------------------

In this case, the statement [ System.out.println("print")] will not be consider within the block of if loop, this will be considered after the if loop.
-----------------
if (true) {
return;
System.out.println("print");
}
--------------------
if the curly braces r given,then the codes leads to unreachable statement error.

if the curly braces is not specified ,the if loop will take only single statement (that is,the first stmt that is present immediately under if ) the others lines are considered to be the program continuation and not a part of if block.

regards...
sakthi
 
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For those who are interested, the long, formal description of what does and does not constitute an unreachable statement can be found in the JLS here.
 
Ravi Tanuku
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Joe. I found the answer to my question from JLS.
Here the point is, in case variable initialization, the compiler checks if the variable is definitely assigned before its used. The statement makes i definitely assigned as compiler can recognize that i=10 is always executed.
In case of unreachable code, in the code , even if the compiler knows that the statement after return is not reachable it doesn't report error. This is a specail treatment for "if" while checking unreachable code. It allows conditional compilation. However the following code will give unreachable code error
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because 'true' is a constant expression (JLS 15.28).

Try this:
boolean b = true; // not a constant expression.
if(b){return;}blah(); // no complaints from the compiler.

final boolean b = true; // A constant expression.
if(b){return;}blah(); // compiler complains.
 
Joe Sondow
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tony Morris:
Because 'true' is a constant expression (JLS 15.28).

Try this:
boolean b = true; // not a constant expression.
if(b){return;}blah(); // no complaints from the compiler.

final boolean b = true; // A constant expression.
if(b){return;}blah(); // compiler complains.



Actually, neither of those code samples cause a compile-time error, because there are special rules for "if" statements when it comes to unreachable statements.



The case where the compiler complains about the unreachable statement "blah();" is when it's a while loop, not an if statement, and the boolean test value is a constant value. Both of the following two examples cause compiler errors:



And the following example compiles fine because the boolean test is not a constant expression:

 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad.
s/if/while
 
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic