Are the conditional statements if(true) and if(false) evaluated at compile time in java?

263 views Asked by At

The following code compiles successfully:

int x;
if(true)
  x=3;
System.out.println(x);

Does that mean the if condition is evaluated at compile time? If so why does the following code not throw Unreachable Statement error?

if(true)
  return;
return;  //No error

What's the difference?

Edit: Please note that my query is different as I am not comparing 'if' with 'while'. Rather I am comparing the same if(true) statement in two different situations, in the first one it appears to be evaluated at compile time, while in the 2nd case, it appears to be evaluated at runtime.

0

There are 0 answers