How does a compiler decide on with unreachable code?
consider these
public class Test15 {
public static final boolean verdict = false;
public static final int verdictInt = 2;
public static void main(String[] args) throws IOException {
// case1
if (verdict) {
System.out.println(1);
} else {
System.out.println(2);
}// compiles
//case 2
int val = 5;
switch (val) {
case verdictInt:System.out.println(3);
break;
}// compiles
//case 3
while (false) {
}// does not compile
//case 4
return 6;
return 7;
//does not compile
}
}
So when the compiler decides that code is unreachable.Is it because of (for a lack of a better term) hard code(like case 4).Because as per my knowledge, verdict,verdictInt also qualify as compile time constants and according too. And as per my reading,being able to use the constant verdictInt as a switch case label indicates that it is a compile time constant.
Please let me know in case there are any basic flaws in my reasoning.
The Java Language Specification section on unreachable code is a good, but long read. I'll condense it into what translates for your question.
verdict
could be seen as a flag variable; it may or may not be used for debug purposes. If it is, then breaking compilation would result in a major design goal miss. The specification explicitly calls this out:
The rationale for this differing treatment [with
if
as opposed towhile
] is to allow programmers to define "flag variables" such as:static final boolean DEBUG = false;
and then write code such as:
if (DEBUG) { x=3; }
The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.
Your switch
statement can complete normally by the rules of the JLS:
A
switch
statement can complete normally iff at least one of the following is true:
- The
switch
block is empty or contains only switch labels.- The last statement in the
switch
block can complete normally.- There is at least one
switch
label after the lastswitch
block statement group.- The
switch
block does not contain adefault
label.- There is a reachable
break
statement that exits theswitch
statement.
Your switch
doesn't have a default
label, so your switch can complete normally. It just falls out of the switch.
This explicitly does not compile. Case 1 called some attention to it, but here it's spelled out, emphasis mine:
A
while
statement can complete normally iff at least one of the following is true:
The
while
statement is reachable and the condition expression is not a constant expression (§15.28) with valuetrue
.There is a reachable
break
statement that exits thewhile
statement.The contained statement is reachable iff the
while
statement is reachable and the condition expression is not a constant expression whose value isfalse
.
The condition expression is false
, so you can't reach the contained statement, by definition.
return
is a special case; it completes abruptly. Once a statement completes abruptly, any statements after that are not reachable by definition.