posted 20 years ago
I was surprised to find out that the compiler is smart enough to know that an infinite while loop makes the code below it unreachable if the condition is the literal true or a final boolean variable set to true, but not a boolean variable that is set to true just before the loop. For example:
5. while(true);
6. doIt();
or
5. final boolean b = true;
6. while(b);
7. doIt();
will both fail to compile, while
5. boolean b = true;
6. while(b);
7. doIt();
is acceptable. I was also surprised that it does not know that a second assert false is unreachable, e.g. compiling this with assertions on:
5. assert false;
6. assert false;
does not complain about the second assert. Can anyone give me a concise rule about when the compiler is smart about unreachable code and when it is not, as opposed to just knowing the cases?