Folks, I am having a question on Unreachable code. if (2<1)<br /> //<br /> else<br /> //<br /> Here the if part can never be true and the // following if can<br /> never be reached, or it is a unreachable code. why does it not show a compilation error. Also if a final variable id put in place of constant say<br /> final int i = 2;<br /> if(i > 3) // This // is also unreachable right? why it does not give a error.
------------------ Regards, V. Kishan Kumar
Regards,<BR>V. Kishan Kumar
Ramesh Donnipadu
Ranch Hand
Joined: Sep 16, 2000
Posts: 100
posted
0
Kishan, I think java compiler does not perform rigorous analysis of code. Try this. if (true) { System.out.println("Always true"); } else { System.out.println("Sometimes false"); }
Kishan Kumar
Ranch Hand
Joined: Sep 26, 2000
Posts: 130
posted
0
Ramesh, Even that is not giving a error, but logically speaking any code that is unreahable should not be accepted. In case of exceptions it promptly gives the unreachable code error. ie, if the superclass exception is caught ahead of subclass exception it gives this unreachable code error. Likewise if(2<1) is known at compiletime itself and this sentence does not have a meaning at all, but in Java's point of view why is it legal. Is it a bug that deserves enough for Sun people to know?. or is there any concrete reason for this to happen. Final variables I can understand that at compile time the value may not be known as their values are initialised in constructors and static initializers. Any answers... ------------------ Regards, V. Kishan Kumar