• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assertion question

 
Dan Xu
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Philip Heller
author
Ranch Hand
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More on assertions and when to use them in Chapter 5 of "Complete Java 2 Certification".

-- Phil
 
Dan Xu
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only have this one
JAVA 2 sun certified programmer & developer
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Programming with Assertions (part of SDK documentation)
 
Kanchan K Bulbule
Greenhorn
Posts: 20
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 99
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic