Search code examples
javaunreachable-code

When does the condition "Unreachable Code" occur in Java?


When there is some statement written after the infinite loop, that statement becomes the unreachable code. For ex:

for(;;) 
{
}
Sytem.out.println("Test-1"); //unreachable code

But I am facing some difficulty here.

Look at the two code snippets below:

Code snippet1:

for(final int z=4;z<6;)
{
}
System.out.println("Test-2"); //unreachable code

Here, The last statement must be unreachable because the loop is infinite and the output is as expected.

Code Snippet2:

final int z=4;
for(;;)
{
    if(z<2)
        break;
}
System.out.println("Test-3");  //not unreachable

Conceptually, the for loop in above code is also infinite since z is final and if(z<2) is determined at compile time only.The if condition will never be true and the loop will never break. But, the Last statement in above code is not unreachable.

Questions:

  1. Why this is happening ?

  2. Can anyone tell me the exact rules by which we can see whether code is unreachable or not.


Solution

  • The key phrase in http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.21 is:

    It is a compile-time error if a statement cannot be executed because it is unreachable.

    This section is devoted to a precise explanation of the word "reachable." The idea is that there must be some possible execution path from the beginning of the constructor, method, instance initializer, or static initializer that contains the statement to the statement itself. The analysis takes into account the structure of statements. Except for the special treatment of while, do, and for statements whose condition expression has the constant value true, the values of expressions are not taken into account in the flow analysis.

    Hence the compiler does not evaluate z<2 in your if() statement, and does not know that it will never evaluate to true.

    This defines unreachable code as far as the Java spec is concerned. It's important that compilers adhere to to the spec, because changing the rules could make code that used to compile fail to compile.

    However, compilers are free to give warnings rather than compilation errors.

    If I type the following code into Eclipse:

    final int x = 0;
    if(x == 1) {
        System.out.println("This never happens");
    }
    

    ... I get the warning "Dead code". The compiler knows the code can't be reached - but it can't refuse to compile, because the code is not formally "unreachable" according to the Java spec.