From JLS 14.21. Unreachable Statements
It is a compile-time error if a statement cannot be executed because it is unreachable.
and
The else-statement is reachable iff the if-then-else statement is reachable.
Your if-then-else statement is reachable. So, by the definition the compiler thinks that the else-statement is reachable.
Note: Interestingly the following code also compiles
// This is ok
if (false) { /* do something */ }
This is not true for while
// This will not compile
while (false) { /* do something */ }
because the reachability definition for while
is different (emphasis mine):
The contained statement is reachable iff the while statement is reachable and the condition expression is not a constant expression whose value is false.
if
andelse if
are reachable, theelse
statement is it as well. – Quarantinenull
? – TrapezohedronBoolean
which is object.boolean
is primitive. – Alviedefault
cases in switches where all possibilities already covered. Why shouldboolean
be special just because there are only 2 values? – Finalismif (a == true)
,if (a == false)
<-- Don't ever do this whena
is aboolean
. Just doif (a)
andif (!a)
. – Scalpa == true
without there being a very good reason why screams that the author lacks understanding about what==
does. I admit that there are rare cases where it might make sense, but these are extremely rare. (They might be a bit more common in weakly or dynamically typed languages, but this is Java.) Certainly, in this example, it's completely unnecessary. – Scalpif
and the parentheses and thata
is aboolean
(primitive). It never makes sense in the full context (a single boolean check on a non-nullable type). – Scalp