I have been observing that conditional expressions in Java that are formed only by literals or constants (or operations between them) are evaluated in different ways. So, the code below compiles normally:
public class Main {
public static void main(String[] args) {
if (true) {
System.out.println("teste");
}
while (true) {
System.out.println("teste");
}
}
}
However, this section does not compile. The compiler complains of "Unreachable code" only in the while
part, the part within if
is also "unreachable".
public class Main {
public static void main(String[] args) {
if (false) {
System.out.println("teste");
}
while (false) {
System.out.println("teste");
}
}
}
I wonder if it is possible to make "unreachable codes" within if
blocks as in the example above also be treated as compile-time errors. Does anyone know of any flag I can pass to the compiler for this to happen? (if that flag exists) Or a similar mechanism?