As a point of interest, unreachable code is always an error in
Java. Unreachable code means that there is no path of execution that will cause the code to execute. This can occur a number of different ways, including a misplaced break statement, as is the case with your code. Here is another example:
class Opps {
public static void main(
String args[]) {
int i = 99;
if(i < 100) {
System.out.println("Less than 100.");
return;
} else {
System.out.println("Greater than or equal to 100.");
return;
}
System.out.println("This is unreachable!");
}
}
In this program, both paths of execution through the if/else end with a return. Thus, the last call to println() is unreachable. That is, there is no path of execution that can lead to the final println().
Keep in mind that javac can't find all unreachable code. It can only find code that is unreachable because there is no path of execution that can possibly lead to the unreachable code. Code that is unreachable because of logic errors in your program won't be found. For example, assume that your program has a switch statement that contains a case value that will never occur during the execution of the program. This case statement is NOT flagged as unreachable because the compiler has no way of knowing that this case value will never occur at runtime.