質問

Possible Duplicate:
Why is this code giving an “Unreachable Statement” error?

This seems very easy question, I found this question in one book. If anyone help me to figure out why I'm getting error.

    do {
        System.out.print("inside do");
    } while (false);
    while (false) { // error
        System.out.print("inside while");
    }
    System.out.print("outside");

I thought, and according to me, output should be inside dooutside. But, it is showing Compiler Error : Unreachable Statement. Then, I tried to figure out, why, it is showing Compilation error : Unreachable Statement* . So, I change the above code like this

  boolean i = false;  
  do {
        System.out.print("inside do");
    } while (false);
    while (i) { // ok
        System.out.print("inside while");
    }
    System.out.print("outside");

Now, it is showing expected output i.e. inside dooutside . So, my question is - what makes difference in first and second case ? Also, when I check

if(false){ 
  //something here
   }

Then, above code executes without any error.

役に立ちましたか?

解決

The main difference between the first two examples is that in the first case, the condition is a constant, whereas in the second it is not.

For example, if you change boolean i = false; into final boolean i = false;, you will get the same compile error because i is now a constant.

The rules for unreachable statements are defined in the JLS 14.21. In particular there is a special treatment for if to allow if(DEBUG) structures where DEBUG might be a constant.

As for the do / while, the statement inside will be executed once so there is no problem.

More details about the constants in this related post.

他のヒント

The compiler is giving you an Unreachable statement error because your System.out.print("inside while"); code can never be reached with

while (false) { // error
    System.out.print("inside while");
}

The compiler knows that while (false) will never be true, and warns you (with an error) about the dead code.

In contrast, if you put a variable in the while instead, because the nature of variables is that they can vary (change), the compiler's static analysis doesn't identify unreachable code. (Even in many situations where you or I could look at it and say "that code's never going to be run"; the compiler's analysis is fairly shallow, that's not it's main job. There are more beefy analysis and code-coverage tools you can use.)

In regards if (false), here's what the JLS has to say about it:

As an example, the following statement results in a compile-time error:

while (false) { x=3; }

because the statement x=3; is not reachable; but the superficially similar case:

if (false) { x=3; }

does not result in a compile-time error. An optimizing compiler may realize that the statement x=3; will never be executed and may choose to omit the code for that statement from the generated class file, but the statement x=3; is not regarded as "unreachable" in the technical sense specified here.

The rationale for this differing treatment is to allow programmers to define "flag variables" such as:

static final boolean DEBUG = false;

and then write code such as:

if (DEBUG) { x=3; }

The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top