wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Unreachable Code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Unreachable Code" Watch "Unreachable Code" New topic
Author

Unreachable Code

Ravi Tanuku
Greenhorn

Joined: Apr 06, 2005
Posts: 9
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

Joined: Dec 13, 2004
Posts: 24
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

Joined: Mar 29, 2005
Posts: 98
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


" Don't be afraid of pressure. Remember that pressure is what turns a lump of coal into a diamond... " <br /> <br />Thanks & Regards...<br />Sakthi<br />SCJP1.4, OCA
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
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.


SCJA 1.0 (98%), SCJP 1.4 (98%)
Ravi Tanuku
Greenhorn

Joined: Apr 06, 2005
Posts: 9
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

Joined: Sep 24, 2003
Posts: 1608
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.


Tony Morris
Java Q&A (FAQ, Trivia)
Joe Sondow
Ranch Hand

Joined: Apr 10, 2005
Posts: 195
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

Joined: Sep 24, 2003
Posts: 1608
My bad.
s/if/while
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unreachable Code
 
Similar Threads
Qustion in MockTest about assertions
compiler-time condition
about statement unreachable
unreachable code?
clarification on why the modifier private changes output..