In the following code, while compiling by command,
javac tst.java or javac -source 1.4 tst.java
I am getting error at line numbered 7. Error is "unreachable statement". If line 7 is commented, then program compiles.
My question is : Why we are not getting same error for line no.4. Because, my assumption is after execution of line no.3, program will throw AssertionError and control will never touch to line no. 4? ====================================================
(Formatted a favor to the other members of this forum) [ November 03, 2004: Message edited by: Barry Gaunt ]
"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
Line 7 will not be reached because of the infinite loop. I think the compiler does not take the assert false statements into account because it must assume that at run time assertions could be disabled.
That's just an educated guess though, so do not take my word for it.
Thank you Barry, I can understand error at line no. 7. I do not understand, for same assert false; statement at line no.4 Why compiler is not giving error?
Joined: Aug 03, 2002
If assertions are disabled at runtime those two assert false statements are effectively not there. So the program will run ok at that point. So the compiler cannot complain that the second assert false (line 4) is unreachable. The compiler doesn't know about the runtime status of assertions (enabled or disabled).
Joined: Sep 09, 2004
Thank you Barry, i understand your point. Your point is applicable to all assertion statements. So, my question still remains same..
Your explanation as :
"If assertions are disabled at runtime those two assert false statements are effectively not there. So the program will run ok at that point.So the compiler cannot complain that the second assert false (line 4) is unreachable. "
-- This is also applicable to assertion statement at line no.6 and 7. Then why it is giving error only for line 7 and not for 4.
"The compiler doesn't know about the runtime status of assertions (enabled or disabled)."
-- How compiler know about runtime status of assertions about line no.7. User can disable assertions and may run program. So, line no. 7 will have no effect on program.
Please help me to understand these points. Thank you again.
Joined: Aug 03, 2002
I'm sorry I do not know how to explain better than I previously did.
the above code is not related to the assertion status.., since the while loop specifies as "while(true)" this becomes infinite loop thereby any statement below this is "unreachable" - you can try with simple java code other than assertion, will results "unreachable statement"