Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

assertion question

 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Nitin Bhagwat
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm sorry I do not know how to explain better than I previously did.
 
Joe Borderi
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"[I]-- 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.[I]"

May I suggest replacing the assertion and substituting any viable executable statement at line 7? Now try compiling.
 
Arshad Ahamed
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic