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?
You must read the Unreachable Statements. Although with
while(false)
the compiler will throw an error but withif(false)
it wil show a warning to the user.Although
if (false)
was kept in Java to simulate C/C++ preprocessor#if 0
The specification says that:
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 constructif(false)
is permitted for that purpose.