permaculture playing cards*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertion question" Watch "Assertion question" New topic
Author

Assertion question

Dan Xu
Greenhorn

Joined: Nov 29, 2002
Posts: 15


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
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
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
I only have this one
JAVA 2 sun certified programmer & developer
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Programming with Assertions (part of SDK documentation)


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Kanchan K Bulbule
Greenhorn

Joined: Jul 19, 2005
Posts: 20
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.


For more detailed information. Kindly refer to the sample chapter by Khalid Mohamad on assertion at
http://www.ii.uib.no/~khalid/pgjc/jcbook/new-exam-note-020928.html

Hope this answers your queries.

Cheers
Kanchan
Preparing for SCJP 1.4
Smitha Ballikar
Ranch Hand

Joined: Aug 02, 2005
Posts: 99
Hi Dan,

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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Assertion question