Ans:With assertions enabled it prints 210210 followed by an AssertionError message. With assertions disabled it prints 210210-1
--really need someone to explain it to me.can't understand. and if i want to complie,what's command i need to input
How to fix the following error to make the program work? Asser.java:10: warning: as of release 1.4, assert is a keyword, and may not be used as an identifier assert j == 2; ^ Asser.java:10: ';' expected assert j == 2;
Thanks a lot.
Vasanth Raja.K.
Ranch Hand
Joined: May 30, 2005
Posts: 35
posted
0
Hi Dan, If u want to use assertion u have to put javac -source 1.4 <Source File Name>.
For Execution java -ea <Class Name> or java -enableassertions <Class Name>
Syntax for Assertion is: assert <boolean expression>:expression If the boolean expression is false and the assertions enabled,expression value is passed to Assertion Error Constructor.This value is converted into string format when assertion fails.
Hope u understood. Reg Vasanth
SCJP 1.4(85%)<br />SCWCD 1.4(79%)
Philip Heller
author
Ranch Hand
Joined: Oct 24, 2000
Posts: 119
posted
0
More on assertions and when to use them in Chapter 5 of "Complete Java 2 Certification".
-- Phil
Consultant to SCJP team.<br />Co-designer of SCJD exam.<br />Co-author of "Complete Java 2 Certification Study Guide".<br />Author of "Ground-Up Java".
Dan Xu
Greenhorn
Joined: Nov 29, 2002
Posts: 15
posted
0
I only have this one JAVA 2 sun certified programmer & developer
210210 get printed for the values of i from 5 to 0 .
When i=-1 , it goes into the default case of the switch statement. and it fails at the assertion ie assert i==2 and hence it gives the assertionError (when assertion are enabled ) and thus terminates .
when we disable assertion the assert command is not executed and system print the -1 value of the i.
If you had queries in compiling and running this program then I think it is answered by Vasanth.
However regarding the logic,lets go through it step by step
When i=5,j=5%3=2 In switch construct, default is executed since none of the case labels match.Here since assertion is enabled assert condition is checked which is true(j==2) and 2 is printed.
when i=4,j=4%3=1 In switch construct , the matching case is executed i.e case 1 and 1 is printed.
when i=3,j=3%3=0 In switch construct , the matching case is executed i.e case 0 and 0 is printed.
when i=2,j=2%3=2 In switch construct, default is executed since none of the case labels match.Here since assertion is enabled assert condition is checked which is true(j==2) and 2 is printed.
when i=1,j=1%3=1 In switch construct , the matching case is executed i.e case 1 and 1 is printed.
when i=0,j=0%3=0 In switch construct , the matching case is executed i.e case 0 and 0 is printed.
when i=-1 j=-1%3=-1 default is executed since j==2assertion fails and assertion error is thrown
Similarly if assertion is disabled the same thing happens only -1 is printed and assertion error is not thrown
So output is 210210 Assertion Error(In assertion enabled) and 210210 -1 (if assertion disabled)
Hope you got it!!
Cheers Smitha
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.