10
class For1
{
  public static void main(String args[])
  {
    int a = 0;
    for(;;)
    {
      break;
      System.out.println(a); //Line 1
      ++a;//Line 2
    }
  }
}

I know that Line 1/Line 2 will never be executed. But still I don't understand why a compile time error is thrown. I am getting "unreachable statement" compile error.

Does it mean that compiler checks whether it is able to compile for all branches/lines of code ?

Dariusz
  • 21,561
  • 9
  • 74
  • 114
UnderDog
  • 3,173
  • 10
  • 32
  • 49

6 Answers6

15

Does it mean that compiler checks whether it is able to compile for all branches/lines of code ?

It means the compiler checks that every statement is reachable.

From section 14.21 of the JLS:

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.

The section then documents how reachability is defined.

In particular, the relevant points in your case are:

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 break, continue, return, or throw statement cannot complete normally.

So your "line 1" statement is preceded by a statement (break;) which cannot complete normally, and therefore it's unreachable.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • This block inside if statement here(https://ideone.com/pMRqwz) will never be reached. But java compiler doesn't throw `unreachable statement error` – Siva Praveen Aug 21 '18 at 11:48
  • @SivaPraveen Yes, because on pure language terms, the statement isn't unreachable. We can tell it will never be reached, but that's not the same as the language rules making it unreachable. – Jon Skeet Aug 21 '18 at 15:10
  • Thank You. The statement is unreachable and the program runs fine. But, that's a useless part in our code. a couple of questions? 1. where can I see the java rules 2. Is there any library which can identify all kinds of unreachable statements? – Siva Praveen Aug 23 '18 at 11:46
  • @SivaPraveen: The Java Language Specification is at https://docs.oracle.com/javase/specs/jls/se10/html/index.html. Identifying *all* kinds of unreachable statements would be a Turing-complete task, I suspect - but there may well be tools that help identify *common* issues. I don't know of them off-hand though - I'd be doing the same kind of research that you can. – Jon Skeet Aug 23 '18 at 18:50
7

The compiler is also able to make that conclusion, and assumes you are making a mistake. And yes, the Java compiler does a pretty good amount of "Data-Flow Analysis". The most common related message is the one about variables not initialized. The second most frequent is, I believe, precisely this one, about code not reachable.

Mario Rossi
  • 7,651
  • 27
  • 37
4

Does it mean that compiler checks whether it is able to compile for all branches/lines of code ?

Yes compiler compiles the whole body of code and make byte code according to your code, it smarter enough to detects unreachable code also dead code. Immediate break in the for-loop makes unreachable other statements.

for(;;){
   break;
   ... // unreachable statement
}


int i=1;
if(i==1)
  ...
else
  ... // dead code
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
3

Unreachable code is meaningless and redundant. If you have some unreachable code in your program it is a mistake and needs to be fixed. Hence compiler throws an error.

You can refer to similar questions below

Unreachable code: error or warning? and Why does Java have an "unreachable statement" compiler error?

Community
  • 1
  • 1
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
2

The compiler is able to determine that these two statement will never, ever be executed, and helps you write correct code by refusing to compile it, because this has 99.9% chance of being an error rather than a conscious choice to add statements that will never be executed.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
1

The compiler will check if there is more code after certain keywords. Another keyword which will cause a similar message is if you replace break by return.

npinti
  • 51,780
  • 5
  • 72
  • 96