This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Hello! Could someone be so kind to explain when will the compiler give an error for not being able to reach the code. For example why is there is no compiler error in the below code? void ifcheck(boolean flag) { if (flag) if (flag) System.out.println("A"); else System.out.println("B"); // this line does not get executed else System.out.println("C"); }
}
Rob Levo
Ranch Hand
Joined: Oct 01, 2000
Posts: 167
posted
0
Originally posted by Charlie Swanson: Hello! Could someone be so kind to explain when will the compiler give an error for not being able to reach the code. For example why is there is no compiler error in the below code? void ifcheck(boolean flag) { if (flag) if (flag) System.out.println("A"); else System.out.println("B"); // this line does not get executed else System.out.println("C"); }
}
I am not suprised that the compiler does not complain about not being able to reach some code in your example. It can not catch all the "tricks", like the one in your example to make some code unreachable. I can not anwser your specific question regarding when the complier will find unreachable code and when it will not. Do not know about the compiler to that level of detail. Compiler is a complicated program. I doubt you will get your answer unless you find someone with the "nuts and bolts" knowledge of how everything is done.
Charlie Swanson
Ranch Hand
Joined: Jan 29, 2001
Posts: 213
posted
0
The only reason why I am asking the above question is that it was a question in the JQPlus exam; and I assumed I must be aware of it for the test. The question does seem a bit detailed (nuts and bolts) for the exam.
tvs sundaram
Ranch Hand
Joined: Jan 28, 2001
Posts: 153
posted
0
<pre> void ifcheck(boolean flag) { if (flag) if (flag) System.out.println("A"); else System.out.println("B"); // this line does not get executed else System.out.println("C"); } } </pre> The code can be grouped like this ; if so, no unreachable statement in this code. Guru's pls correct me if iam wrong.. Thanks
Rob Levo
Ranch Hand
Joined: Oct 01, 2000
Posts: 167
posted
0
Changing the indentation does not change the fact that the statement is unreachable. I am surprised that this question appeared on JQ. Hopefully these low level compiler questions do not appear on the real exam.
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
Hi Charlie, The Unreachable Statements rule in JLS�14.20 details special considerations for <code>if-then-else</code> statements.
The rationale for this differing treatment is to allow programmers to define "flag variables" such as: static final boolean DEBUG = false; and then write code such as: if (DEBUG) { x=3; } The idea is that it should be possible to change the value of DEBUG from false to true or from true to false and then compile the code correctly with no other changes to the program text.
Hope that helps. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform [This message has been edited by Jane Griscti (edited May 12, 2001).]
FYI, there isn't any unreachable code in your example. With the use of threads the value of flags could change from the first if(flags) to the second, even in runtime.