jeudi 10 septembre 2015

(Compiler) else if(true) vs else scenario

Take the following Java code snippet:

.... else if (true){ //hard-coded as true ///launch methodA } else { ///launch methodA (same code as in the else if statement) } ....

What I was wondering is how the compiler deals with this. Wouldn't it be logical for the compiler to remove the else if(true) statement altogether in order to not have to perform a check, even though it is hard-coded as true. Specifically in Eclipse, how is the code above interpreted?

Or what about in the following scenario:

.... else if (true){ //hard-coded as true ///launch methodA } else { ///launch methodBB } ....

Wouldn't it be logical in this case for the compiler to remove the else statement? Because while running, the else statement is unreachable.

Aucun commentaire:

Enregistrer un commentaire