I tried the following in Eclipse:
if (false) {}
: warning 'dead code'while (false) {}
: compilation error 'unreachable code'I was wondering whether there is a real 'reason' for this difference. I already found this...
Unreachable code compiler error
...but why not allow while (false)
for the same debugging purpose?
The JLS section on unreachable code explains the rationale. Essentially, Java normally shouldn't use conditional compilation like C routinely does with #ifdef
, but there are some situations (such as debugging, and in particular backward binary compatibility) where allowing the compiler to entirely strip out code is needed, and so the specific construct if(false)
is permitted for that purpose.