vendredi 22 juin 2018

Does the java compiler understand conditions given in if statements?

The following code does not give the compile-error Unreachable statement.

if(true)return;
int x;

For years I believed that it's because the compiler does not pay much attention to the conditions given.

Today I found that the compiler understands the conditions,

int x;
if (true) {
    x = 0;
}
int y = x;

because if not this should result in another compile-error variable x might not have been initialized. Which in fact compiles and runs perfectly. So,

Does the java compiler understand conditions given in if statements?

Aucun commentaire:

Enregistrer un commentaire