This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes assertion question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "assertion question" Watch "assertion question" New topic
Author

assertion question

Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
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."
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
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

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Sep 09, 2004
Posts: 132
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

Joined: Aug 03, 2002
Posts: 7729
I'm sorry I do not know how to explain better than I previously did.
Joe Borderi
Ranch Hand

Joined: Oct 23, 2004
Posts: 151
"[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

Joined: Nov 03, 2004
Posts: 5
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"


Be YourSelf !!
 
 
subject: assertion question
 
Similar Threads
Section 2 mock test questions
assertion
Doubt in Assert statement
Doubt in assert
Assertion