File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unreachable Code

 
Ravi Tanuku
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Abhinay Verma
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Sakthi Kani
Ranch Hand
Posts: 98
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Joe Sondow
Ranch Hand
Posts: 195
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Tony Morris
Ranch Hand
Posts: 1608
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My bad.
s/if/while
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic