Why is this code not giving an "unreachable code" error? Since a boolean can only be true or false.
public static void main(String args[]) {
boolean a = false;
if (a == true) {
} else if (a == false) {
} else {
int c = 0;
c = c + 1;
}
}
From JLS 14.21. Unreachable Statements
and
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 not true for
while
because the reachability definition for
while
is different (emphasis mine):