Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 967
Vikas Mangal Avatar asked Jun 12 '14 09:06

Vikas Mangal


People also ask

What makes a statement unreachable in Java?

The Unreachable statements refers to statements that won't get executed during the execution of the program are called Unreachable Statements. These statements might be unreachable because of the following reasons: Have a return statement before them. Have an infinite loop before them.

Why does it say my code is unreachable?

The JavaScript warning "unreachable code after return statement" occurs when using an expression after a return statement, or when using a semicolon-less return statement but including an expression directly after.

How do I fix unreachable code error in Java?

1(a) is compiled, line 12 raises an unreachable statement error because the break statement exits the for loop and the successive statement cannot be executed. To address this issue, the control flow needs to be restructured and the unreachable statement removed, or moved outside the enclosing block, as shown in Fig.

How do you find an unreachable code?

While some simple cases of unreachable code can be detected by static analysis (typically if a condition in an if statement can be determined to be always true or false), most cases of unreachable code can only be detected by performing coverage analysis in testing, with the caveat that code reported as not being ...


2 Answers

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.

like image 193
slim Avatar answered Oct 17 '22 09:10

slim


From http://docs.oracle.com/javase/specs/jls/se7/html/jls-14.html#jls-14.21

14.21. Unreachable Statements

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.

For example, a Java compiler will accept the code:

{ int n = 5; while (n > 7) k = 2; } even though the value of n is known at compile time and in principle it can be known at compile time that the assignment to k can never be executed.

The rules in this section define two technical terms:

whether a statement is reachable

whether a statement can complete normally

The definitions here allow a statement to complete normally only if it is reachable.

To shorten the description of the rules, the customary abbreviation "iff" is used to mean "if and only if."

A reachable break statement exits a statement if, within the break target, either there are no try statements whose try blocks contain the break statement, or there are try statements whose try blocks contain the break statement and all finally clauses of those try statements can complete normally.

This definition is based on the logic around "attempts to transfer control" in §14.15.

A continue statement continues a do statement if, within the do statement, either there are no try statements whose try blocks contain the continue statement, or there are try statements whose try blocks contain the continue statement and all finally clauses of those try statements can complete normally.

The rules are as follows:

The block that is the body of a constructor, method, instance initializer, or static initializer is reachable.

An empty block that is not a switch block can complete normally iff it is reachable.

A non-empty block that is not a switch block can complete normally iff the last statement in it can complete normally.

The first statement in a non-empty block that is not a switch block is reachable iff the block is reachable.

Every other statement S in a non-empty block that is not a switch block is reachable iff the statement preceding S can complete normally.

A local class declaration statement can complete normally iff it is reachable.

A local variable declaration statement can complete normally iff it is reachable.

An empty statement can complete normally iff it is reachable.

A labeled statement can complete normally if at least one of the following is true:

The contained statement can complete normally.

There is a reachable break statement that exits the labeled statement.

The contained statement is reachable iff the labeled statement is reachable.

An expression statement can complete normally iff it is reachable.

An if-then statement can complete normally iff it is reachable.

The then-statement is reachable iff the if-then statement is reachable.

An if-then-else statement can complete normally iff the then-statement can complete normally or the else-statement can complete normally.

The then-statement is reachable iff the if-then-else statement is reachable.

The else-statement is reachable iff the if-then-else statement is reachable.

This handling of an if statement, whether or not it has an else part, is rather unusual. The rationale is given at the end of this section.

An assert statement can complete normally iff it is reachable.

A switch statement can complete normally iff at least one of the following is true:

The switch block is empty or contains only switch labels.

The last statement in the switch block can complete normally.

There is at least one switch label after the last switch block statement group.

The switch block does not contain a default label.

There is a reachable break statement that exits the switch statement.

A switch block is reachable iff its switch statement is reachable.

A statement in a switch block is reachable iff its switch statement is reachable and at least one of the following is true:

It bears a case or default label.

There is a statement preceding it in the switch block and that preceding statement can complete normally.

A while statement can complete normally iff at least one of the following is true:

The while statement is reachable and the condition expression is not a constant expression (§15.28) with value true.

There is a reachable break statement that exits the while statement.

The contained statement is reachable iff the while statement is reachable and the condition expression is not a constant expression whose value is false.

A do statement can complete normally iff at least one of the following is true:

The contained statement can complete normally and the condition expression is not a constant expression (§15.28) with value true.

The do statement contains a reachable continue statement with no label, and the do statement is the innermost while, do, or for statement that contains that continue statement, and the continue statement continues that do statement, and the condition expression is not a constant expression with value true.

The do statement contains a reachable continue statement with a label L, and the do statement has label L, and the continue statement continues that do statement, and the condition expression is not a constant expression with value true.

There is a reachable break statement that exits the do statement.

The contained statement is reachable iff the do statement is reachable.

A basic for statement can complete normally iff at least one of the following is true:

The for statement is reachable, there is a condition expression, and the condition expression is not a constant expression (§15.28) with value true.

There is a reachable break statement that exits the for statement.

The contained statement is reachable iff the for statement is reachable and the condition expression is not a constant expression whose value is false.

An enhanced for statement can complete normally iff it is reachable.

A break, continue, return, or throw statement cannot complete normally.

A synchronized statement can complete normally iff the contained statement can complete normally.

The contained statement is reachable iff the synchronized statement is reachable.

A try statement can complete normally iff both of the following are true:

The try block can complete normally or any catch block can complete normally.

If the try statement has a finally block, then the finally block can complete normally.

The try block is reachable iff the try statement is reachable.

A catch block C is reachable iff both of the following are true:

Either the type of C's parameter is an unchecked exception type or Throwable; or some expression or throw statement in the try block is reachable and can throw a checked exception whose type is assignable to the parameter of the catch clause C.

An expression is reachable iff the innermost statement containing it is reachable.

See §15.6 for normal and abrupt completion of expressions.

There is no earlier catch block A in the try statement such that the type of C's parameter is the same as or a subclass of the type of A's parameter.

The Block of a catch block is reachable iff the catch block is reachable.

If a finally block is present, it is reachable iff the try statement is reachable.

like image 2
Andres Avatar answered Oct 17 '22 08:10

Andres