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

The following code compiles successfully:

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

      

Does this mean that the if condition is evaluated at compile time? If so, why doesn't the following code throw an Unreachable Statement error?

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

      

Who cares?

Edit: Please note that my query is different from the fact that I am not comparing "if" with "while". Rather, I am comparing the same if (true) statement in two different situations, in the first it is evaluated at compile time, and in the second case it is evaluated at runtime.

+3
java compiler-optimization if-statement while-loop


source to share


No one has answered this question yet

See similar questions:

25
if (false) vs. while (false): unreachable code and dead code

or similar:

870
Swift Beta: Sorting Arrays
379
Stacking multi-line conditions in if statements?
240
How can I convert ArrayList containing integers to primitive int array?
219
Java method with compiled return type without return statement
209
Python way to avoid "if x: return x"
185
Returning null as int is allowed with ternary operator, but not with operator
184
Missing return statement in non-void compiler
fourteen
Although (false) causes compilation error for invalid statements
0
Obfuscation during blocking
0
Why does the compiler recognize while (true) at compile time but not if (true)



All Articles
Loading...
X
Show
Funny
Dev
Pics