it obviously has no sense as the System.out.println("1"); as well as all other code in this block will be never executed. Just a curious, is there any situation when it will be?
I should say that the final else is unreachable code because there is an elsif flag and an elsif !flag before that last else.
Why does the compiler not signal this unreachable code?
Yes ok. But my point was that the statement
System.out.println("4")
is unreachable code, because on if (!flag) the result will always be true, which is not detected by the compiler.
devlam wrote:Yes ok. But my point was that the statement
System.out.println("4")
is unreachable code, because on if (!flag) the result will always be true, which is not detected by the compiler.
1. flag is a variable. Compiler cannot assume its value even if it is evident by looking at the code. Compiler can only use the values of compile time constants to determine if the code is unreachable or not.
2. if statement is granted an exception by the language designers. Please read this: http://docs.oracle.com/javase/specs/jls ... #jls-14.21
HTH,
Paul.
If you like our products and services, please help us by posting your review here.